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

notflip's avatar

Change the format of a date before saving?

I have a date that's being selected in a datepicker in the following format using AM/PM

Y-m-d g:i A

When I submit the form offcourse Laravel won't update the record because the format is unknown to my DATETIME field in mysql.

I tried using a Mutator on my Model as following, without success

public function setDateAttribute($value)
{
    $this->attributes['date'] = Carbon::createFromFormat('Y-m-d g:i A', $value)->format('Y-m-d H:i:s');
}

The original value is not being updated. Any idea what I might be doing wrong?

Thank you

0 likes
3 replies
frezno's avatar

imho you have to change the the time field in your database to fit your format.

smnhunt's avatar

Are you casting the field to a date in the $dates array of your model?

Please or to participate in this conversation.