Fabio Santos
Fabio Santos

Reputation: 253

laravel 5 format datetime

I have an array that return the following date time:

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

And I need this outuput:

"2016-08-10T13:15:00.000+10:00"

Exist any function to convert this date?

Upvotes: 0

Views: 926

Answers (2)

Alexanderp
Alexanderp

Reputation: 341

Try this:

$dt = new \DateTime('2015-10-28 19:18:44', new \DateTimeZone('Europe/London'));
dd($dt->format('c')); // string '2015-10-28T19:18:44+00:00' (length=25)

Alternatively take a look at Carbon

Upvotes: 2

Ivanka Todorova
Ivanka Todorova

Reputation: 10219

You can use Laravel's accessors to get "reformatted" created_at.

public function getCreatedAtAttribute($value)
{
    //Since Laravel uses Carbon you can do.
    return $value->format('c');
}

This way anytime you do something like $model->created_at it will return modified created_at.

If you want to change datetime format for created_at in your database as well, you can use mutators.

More information you can find on the Laravel's docs page.

Upvotes: 0

Related Questions