Home  >  Q&A  >  body text

Convert string to date in Laravel

I have a string representing a date. I want to store it in my database but the problem is that in my database the property is defined as date. How to correctly convert a string to a date so it can be stored in the database? This is how my string looks like: "fecha":"31 March 2023" . Thanks.

P粉270891688P粉270891688432 days ago802

reply all(1)I'll reply

  • P粉134288794

    P粉1342887942023-09-08 09:58:17

    You can use the strtotime() function to convert any English text datetime description to a unix timestamp, and using the date() function you can format a unix timestamp. You can use both like this:

    $date = date('Y-m-d',strtotime($string));

    You can write it into a mutator, or you can check out the date conversion article in the documentation:

    Date Conversion Document

    reply
    0
  • Cancelreply