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

cooperino's avatar

Changing displayed date format - serializeDate not working?

In my model I have $casts to format the date for database insertion:

  protected $casts = [
		'updated_at' => 'datetime:Y-m-d H:i:s',
]

and below that I added serializeDate to display the date in a different format whenever displayed on the website:

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d');
    }

But still, the dates shown are as Y-m-d H:i:s and not Y-m-d

Edit: This is how I actually insert a row with date to the database for example:

$model->updated_at = date('Y-m-d H:i:s');

in the migration this is how it's defined:

    $table->dateTime('updated_at')->nullable();
0 likes
3 replies
davy_yg's avatar
 protected function serializeDate(DateTimeInterface $date)
	{
    	return date_format($date, 'Y-m-d');
 }
1 like
cooperino's avatar

@davy_yg It is not working, it's weird, I noticed that when I convert to JSON then it does show up.. but I couldn't use JSON. What I eventually did was to "hardcode" it:

protected $casts = [
		'updated_at' => 'datetime:Y-m-d H:i:s \U\T\C',
]
Sinnbeck's avatar

Show the code for how you are getting them from the database and how you are showing them

Be aware that you need to use eloquent for this to work

Also be sure to turn off eloquents own timestamps as you are making your own

public $timestamps = false;
1 like

Please or to participate in this conversation.