Search code examples
phplaravelphp-carbon

How can I format separate date and time variables in Laravel


I have a $date and $time fields that are stored in my db, I would like to know how can I format these fields to display them in a different format?

e.g.

2010-05-09 -> 9th May 2010

and

13:00:00 -> 1:00PM

I have tried to use carbon but it returns the following error:

InvalidArgumentException in Carbon.php line 414: Unexpected data found. Data missing


Solution

  • You have to use the createFromFormat method:

    $time = Carbon::createFromFormat('H:i:s', '13:00:00')->format('g:iA');
    

    The first argument is the format of the date in your string.