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

swaskey's avatar

How can I change the default format of a date field?

I don't want to use the exact date format that the Eloquent model returns when model is typecasted as an array.

Here's a quick code example:

public function show( Request $request, $order_id )
{

    $order = Order::find( $order_id );

    if( !$order )
        return response()->json( [ "message" => "There are no Orders by the selected order ID.","code" => 400 ],400 );

    return response()->json( [ "message" => "success","data" => $output,"code" => 200 ], 200 );

}

Here's the data structure / model table:

+------------+--------------+---------------------+
|   Field    |     Type     |       Default       |
+------------+--------------+---------------------+
| id         | int(10)      | None                |
| status     | varchar(255) | NULL                |
| label      | varchar(255) | NULL                |
| order_date | date         | None                |
| created_at | timestamp    | 0000-00-00 00:00:00 |
| updated_at | timestamp    | 0000-00-00 00:00:00 |
+------------+--------------+---------------------+

Here's the received JSON package:

{
    "message": "success",
    "data": {
        "id": "139",
        "status": "NEW",
        "label": "Home Inspection",
        "order_date": "2016-02-24",
        "created_at": "2016-04-27 20:26:41",
        "updated_at": "2016-04-27 20:26:41"
    },
    "code": 200
}

Currently, all dates are sent to the browser as either Y-m-d or Y-m-d H:i:s.
I'd like to send all dates, by default, in a different date format. What's the best way to go about this?

0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

First of all, you can add order_date to the $dates property in the model which will cast it to a Carbon instance (just as the created_at and updated_at fields are).

There is a $dateFormat property which allows you to define the default format for serialized array/JSON representation of a model.

class Thing extends Model {
    protected $dates = ['created_at', 'updated_at', 'order_date'];
    protected $dateFormat = 'Y-m-d';
}
swaskey's avatar

@tykus_ikus

I'm afraid I was quick to jump here. I am experiencing an issue. I thought it was working, but I was mistaken.

I've tried the following three properties:

1. $dateFormat = "r";

2. $dateFormat = "D, d M Y H:i:s O";

3. $dateFormat = "d M Y H:i:s";

The all result in errors, each slightly different.

I looked up the definition for the $dateFormat property. If I've understood it correctly, that property not only affects the json representation in output, but also the format of the date field stored in mysql. If that's true, it presents an issue. MySql DATETIME field stores dates as 'Y-m-d H:i:s' or '2016-04-22 11:38:32'.

Here's an example error message:

Whoops, looks like something went wrong.
InvalidArgumentException in Carbon.php line 425:
The format separator does not match
Trailing data

in Carbon.php line 425
at Carbon::createFromFormat('r', '2016-04-27 00:00:00') ...

Please or to participate in this conversation.