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

alexanderhempel's avatar

Eloquent timestamps custom date format

Hello, I try to customize the Eloquent timestamps format of 'created_at' and 'updated_at' by doing so in my model:


public function setCreatedAtAttribute($date) { return $this->attributes['created_at'] = $date->format('d.m.Y H:i:s'); }

and in the same fashion for updated_at.

This works fine on Insert rsp. on create a new record but on update these methods are not called, ergo updated_at is stored in the default format.

Does somebody know how to save the update_at in a custom format on update?

0 likes
9 replies
alexanderhempel's avatar

I need to store these dates in a custom format because in production i am working with a MSSQL Database and this guy accepts german date formats only.

pmall's avatar

Maybe you can use the saving observer (whit model observers) to convert the dates in a good format before create and before update.

JarekTkaczyk's avatar
Level 53

@alexanderhempel If you want to use this format for all the timestamps of given model, override this method:

// Your model
protected function getDateFormat()
{
    return 'd.m.Y H:i:s';
}

It means, that all the timestamps will be saved and also shown this way - no need for set/get mutators.

Otherwise you get yourself 'a bit' of trouble: 1. calling $model->updated_at will throw error (because Eloquent expects different format)
2. You need to override methods for update or turn off the timestamps, so Eloquent won't try to set freshTimestamp and so on, and so forth (again, Eloquent is going to parse the date using its default format)

Generally I would suggest changing the column to standard timestamp, instead of what you have now.

2 likes
JarekTkaczyk's avatar

@alexanderhempel Just a note - you don't need any event handlers when you use the method I showed you and you will face issues when you fetch the models back from db without changing the default Eloquent behaviour (like in my post above).

alexanderhempel's avatar

I solved the customization by changing the getDateFormat method and it seems that everthing works correctly on insert, update and select. So Eloquent seems to parse the dates based on the customized format.

JarekTkaczyk's avatar

@alexanderhempel Yep, it does. This format is retrieved from the Grammar by default, ie. it is specific for the SQL server, that your connection is using.

JohnAshmore's avatar

I do this before running my queries for json responses

use Carbon\Carbon;
use DateTime;

// ...
Carbon::setToStringFormat(DateTime::ISO8601);

Please or to participate in this conversation.