Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

skcin7's avatar

Did Laravel change the way datetimes types (`created_at`, `updated_at`, etc) are formatted by default in 5.8? I can't figure out where/how datetimes are formatted in API responses.

In the API portions of my Laravel apps, I'm used to datetime properties (such as created_at and updated_at) being formatted something like this:

"created_at": {
    "date": "2018-12-04 00:44:39.000000",
    "timezone_type": 3,
    "timezone": "UTC"
},

However, in a new app I just recently made which uses Laravel 5.8, the datetime properties are being parsed something like this:

"created_at": "2018-11-19T16:37:07.000000Z",

I looked all over and can't find a reason for this discrepancy. I'm guessing it might be something changed in Laravel 5.8? Can somebody please explain to me how/why this happens? I'd like to change all of my datetime properties to use the latter more condensed format so that my API responses can be less bytes.

Also, in both cases I'm using Eloquent Resources and just simply returning a datetimes like:

public function toArray($request)
{
    return [
        'created_at' => $this->created_at,
    ];
}

This is consistent throughout, so I don't see why/where the discrepancy is. I also have $casts = ['created_at' => 'datetime'] in the models of both apps, so it is consistent there too. I don't see/understand why/how the datetimes are being formatted differently in the 2 different Laravel apps. If someone can help explain how/where this happens, it would be helpful and appreciated.

Thanks.

0 likes
2 replies
skcin7's avatar

@DEVMASTER

Quote from Devmaster:
[] The version of Carbon used by the framework is 2.* (it was 1.* for Laravel up to 5.7)

^ I just noticed this. I'm pretty certain that the way the datetimes are formatted differently now has something to do with this. I tried to look into the Carbon source code to see where/how it gets formatted, but I can't find it. If anyone knows and can share, it would be helpful. But regardless, I'm almost certain this is the reason why the datetimes seem to be formatted differently now. Thanks for your response.

Please or to participate in this conversation.