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

Mar55's avatar
Level 1

Changing the format of a date data type in Laravel

I need to get the dates in the format of "Y-m" for my Chart. Actually i'm getting them like in a timestamp format ('Y-m-d:h-m-s"). How can i change it ?

0 likes
1 reply
undeportedmexican's avatar

I'm assuming this is a custom field (not created_at, updated_at field).

What you can do, is cast the field as date. That will always return a new Carbon instance which you can easily manipulate:

protected $casts = [
	YOUR_FIELD_NAME => 'date'
]

Then when you're retrieving the value, just apply the format metho in carbon:

$your_model->date_field->format('Y-m')

Please or to participate in this conversation.