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

Ligonsker's avatar

Casting Eloquent date to another date format doesn't work

Hello,

In my database I have a some_date column which is a date datatype in SQL Server. The dates are stored as YYYY-mm-dd format in the database

I wanted the dates to appear as dd-mm-YYYY so I did the following in my model:

protected $casts = [
    'some_date' => 'date:d-m-Y',
];

But what it does is, it turns the date from YYYY-mm-dd to datetime with the same order:

00:00:00 YYYY-mm-dd

so instead of changing the date format, it leaves it the same and even adds the time, which doesn't exist, so it always displays it as 00:00:00

Why?

Thanks

0 likes
3 replies
Snapey's avatar

Carbon date will always have a time.

Just cast it to a date (no format) and then format it on display

from the docs

When defining a date or datetime cast, you may also specify the date's format. This format will be used when the model is serialized to an array or JSON:

The format you specify in the cast is only when the model is serialized, Otherwise, it lives in your model as a Carbon object

2 likes
Ligonsker's avatar

@Snapey But I cast it to a date: 'some_date' => 'date:d-m-Y', and not datetime: 'some_date' => 'datetime:d-m-Y' , or that's not what you meant?

Snapey's avatar

@Ligonsker try it ?

When you dd the model, what does it show for the date you cast? An object? what type?

Please or to participate in this conversation.