@iraklisg Just change your timezine here.
protected function serializeDate(DateTimeInterface $date)
{
return $date->timezone('your timezone')->format('Y-m-d H:i:s');
}
Be part of JetBrains PHPverse 2026 on June 9 โ a free online event bringing PHP devs worldwide together.
I have created a fresh Laravel 7 application.
php artisan --version
Laravel Framework 7.9.2
The application is configured to my local timezone:
config/app.php
...
'timezone' => 'Europe/Athens',
...
The application is also connected to a MySQL database server (version 5.7)
Now I have a simple User model as follows:
class User extends Model
{
protected $fillable = ['name', 'birthdate'];
protected $dates = ['birthdate'];
}
Note I use a date mutator in order to convert the birthdate column to Carbon instance.
The model is associated with a users table that have been created using migrations, as shown below:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 40);
$table->date('birthdate')->nullable();
});
}
When I save a new model in the database, the birthdate column is created in the expected Y-m-d H:i:s format, e.g
id | name | birthdate
--------------------------------------------------
1 | John Doe | 2000-05-06 15:31:59
Now when I try to serialize the model I get the following response:
$user = User::find(1);
$user->toArray();
=>
[
"id" => 1,
"name" => "John Doe",
"birthdate" => "2020-05-06T12:31:59.000000Z"
]
Note that the bithdate is formatted as ISO-8601, according to the new date serialization introduced to Laravel 7. The Europe/Athens timezone is +3h compared to Zulu time
So far so good...
However If I try to use attribute casting to cast the date formated in "Y-m-d H:i:s" as follows
protected $casts = [
'birthdate' => 'date:Y-m-d H:i:s',
];
and then try to fetch the serialized model, I get the following:
$user = User::find(1);
$user->toArray();
=>
[
"id" => 1,
"name" => "John Doe",
"birthdate" => "2000-05-06 12:31:59", // <-- wrong time, it should be 2000-05-06 15:31:59
]
Note that if instead of using casts, I override the serializeDate method, as shown in documentation works as expected:
// In User model
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
// Then...
$user = User::find(1);
$user->toArray();
=>
[
"id" => 1,
"name" => "John Doe",
"birthdate" => " 2000-05-06 15:31:59", // <-- yay!
]
Why is this happening? Is this the expected behavior using $casts ?
Please or to participate in this conversation.