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

cschoeni's avatar

How change the date format direct in the migration?

Hello Guys Is there a way to change the date format directly in the migration like this?

$table->date('birthdate')->format('d.m.Y');

Thanks for Help

0 likes
6 replies
Jaytee's avatar

I'm pretty sure you can't. It follows the SQL convention (american date format) im pretty sure. You can however use the Carbon\Carbon DateTime Library and do some things like below.

// my example model

$record = Record::findOrFail(1);

return view('my.view', compact('record'));

// my example view

{{ $record->created_at->format('d.m.Y') }}

ctroms's avatar

@cschoeni If you are using Eloquent, you can define your format in the $dateFormat property on your model.

protected $dateFormat = 'd.m.Y';
cschoeni's avatar

Thanks @ctroms, but when i put this in my User Model what i make after this with the Date. When i save a new User is the brithdate also Y-d-m or when i ouput the brithdate in blade came also the format "Y-d-m".

ctroms's avatar
ctroms
Best Answer
Level 15

When you set the dateFormat property, you are defining the format for how dates are stored in the database and how they are formatted when your model is serialized.

When you access your birthdate attribute on the model, you are still going to be given a carbon instance that can be use to format the birthdate in anyway you would like.

Note that dateFormat will also change the format for your created_at, updated_at and deleted_at attributes.

jekinney's avatar

That's why so many date and time libraries! If working with dates was that easy wouldn't need them, plus time zones etc.

Rational db's hold dates and datetime a etc in a strict format period. This actually helps because then the data is consistent and promised format.

If your not sure how it works, please take five minutes and read the MySQL docs on dates and times. It explains a lot and is always good to understand. More so with UNIX time and time zones, but also format. Then head over to the carbon docs and give that a quick read. Parse, create and output dates to string formats. Laravel helps this a lot, but I argue you really need to understand how and why.

For a simple bday date I use set attribute to format the date before insert and get attribute to format when retrieving the date.

1 like

Please or to participate in this conversation.