Home » Laravel » How to Set Timezone in Laravel

How to Set Timezone in Laravel

As you know, local time depends on the time zone. You can configure the time zone for your system or server, for the PHP instance. Laravel uses the UTC timezone by default. But if you want to use a specific time zone in the Laravel application, you should change the timezone in the Laravel settings.

In this short article, I will explain how to change the timezone in Laravel and how to deal with date serialization in Eloquent models.

How to Set Timezone in Laravel Globally

Laravel will ignore the timezone from PHP settings because it has its own timezone settings. You can configure the timezone in the config/app.php file. Just find the timezone line:

You can set the required value here. The directive supports all time zones supported by PHP. You can find the list on the official website. For example, set Europe/Kyiv:

return [ //... "timezone" => "Europe/Kyiv", //... ];

You also can use the .env file to configure the time zone. Just make the timezone line in the config/app.php look like this:

return [ //... "timezone" => \env("TIMEZONE", "UTC"), //... ];

Then, add this line to the .env file:


Don’t forget to clear the config cache:

php artisan config:clear

How to Serialize Dates in Models

If you try to serialize the Model using the toArray() method, you may notice that any timestamps, for example, created_at and updated_at will have a wrong timezone. I will use the Article model from this article. Let’s look at the example:

If you use Carbon for serialization the timezone is correct. But when you use toArray() the time is shifted back by 3 hours. Looks like this method ignores the Laravel timezone. It is not a bug. Laravel’s developers changed serialization rules for the toArray() and toJSON() methods. Now they use ISO-8601 format and always specify time in UTC. You can read more about it here. If you want Eloquent to respect the timezone, you can override the serializeDate() method in the model. For example:

protected function serializeDate(DateTimeInterface $date) { return $date->format("Y-m-d H:i:s"); }

After this, all dates will be serialized with a correct timezone.

How to Set Timezone Dynamically

You may also want to set a timezone for the user dynamically. There is no way to do it globally, so you should convert each date to the user’s timezone. Of course, you can set the timezone using the date_default_timezone_set() function. But it is a bad idea because this can confuse the dates displayed in the logs and the database. Convert each date to the required timezone manually. Carbon has a setTimezone() method that allows setting custom timezone. For example:

Carbon::now() ->setTimezone(new CarbonTimeZone("Europe/Kyiv")) ->format();

You can get the user’s timezone from the database by first requesting it from the user during registration, or you can determine it from the IP address using torann/geoip. Also, you may try using the jamesmills/laravel-timezone package, which allows saving the user’s timezone on login and has a few helpers for converting dates to the correct timezone.

Wrapping Up

In this article, I have explained how to set a timezone in Laravel and deal with date serialization in Eloquent. The time zone is significant when you need to display the date and time to the nearest hour or minute. As you can see, the configuration is very easy.

Leave a Comment