-1

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

InvalidSyntax
  • 9,131
  • 20
  • 80
  • 127

2 Answers2

1

You can always use PHP's strtotime() function and then manipulate the structure using date() as seen below.

$dateString = strtotime("2010-05-09");
$date = date("jS F, Y" ,$dateString);
echo $date;

$timeString = strtotime("13:00:00");
$time = date("g:i A" ,$timeString);
echo $time;

FWIW, I didn't realize you said specifically in Laravel. But I typed it all out, so figured I'd share anyways. Carbon (as mentioned above) may handle this in a similar fashion.

Brandon White
  • 997
  • 9
  • 22
0

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.

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292