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

Brandon.css's avatar

Remove time from date fields

In my Model, I have specified a date field for due_date.

protected $dates = ['due_date', 'created_at', 'updated_at'];

However, everyone on my website when I call due_date I get **2019-07-04 00:00:00**.

Is there a way to force it to just echo the date, without specifying ->format()

0 likes
6 replies
mstrauss's avatar

Check out date mutators: https://laravel.com/docs/5.8/eloquent-mutators#date-mutators

Specifically the Date Formats section:

Date Formats By default, timestamps are formatted as 'Y-m-d H:i:s'. If you need to customize the timestamp format, set the $dateFormat property on your model. This property determines how date attributes are stored in the database, as well as their format when the model is serialized to an array or JSON:

Cronix's avatar

@mstrauss Yes, but that affects them globally instead of just for an individual field. What if he doesn't want the time stripped off of created_at, updated_at or other datetime fields?

Brandon.css's avatar

@CRONIX - Hmm, looks like it doesn't convert to carbon? When I call {{ $record->due_date }} i get

Call to a member function format() on string

Please or to participate in this conversation.