Laravel 5 Carbon format datetime

d3bug3r picture d3bug3r · Oct 29, 2015 · Viewed 222.8k times · Source

I have an array that returns the following date time:

$item['created_at'] => "2015-10-28 19:18:44"

How do I change the date to M d Y format in Laravel using Carbon?

Currently it returns with an error

$suborder['payment_date'] = $item['created_at']->format('M d Y');

Answer

Milan Maharjan picture Milan Maharjan · Oct 29, 2015

First parse the created_at field as Carbon object.

$createdAt = Carbon::parse($item['created_at']);

Then you can use

$suborder['payment_date'] = $createdAt->format('M d Y');