
Laravel is a dynamic, object-oriented programming framework based on PHP for building modern, scalable web applications ranging from basic to complex web solutions. It follows the Model-View-Controller (MVC) architectural pattern, allowing you to create maintainable code and applications with its modern PHP syntax.
This article explains how to set the timezone in Laravel. Setting the correct timezone ensures consistent date and time handling in your application, especially when working with databases, logs, and frontend user timestamps.
Prerequisites
Before you begin, you need to:
- Have access to an existing instance such as Ubuntu 24.04 with an active Laravel project.
Laravel Timezone Formats
Laravel offers built-in support for timezone management, allowing your application to use accurate date and time values across different regions. Laravel primarily handles time zones using two methods.
- Application Timezone: Laravel stores the global default timezone in the
config/app.php
file which influences how Laravel handles date and time across various components like Eloquent, Carbon, and scheduled tasks. - User Timezone: Stores the user's timezone in the database, and retrieves it from user preferences, or by detection via browser settings. Displaying dates in each user's local timezone improves usability, especially in global applications.
Set the Timezone in Laravel
Laravel uses the UTC
timezone by default. Follow the steps below to update the timezone in your Laravel application.
Navigate to your Laravel project's root directory such as
/var/www/laravelapp
.console$ cd /var/www/laravelapp
Open the
.env
file to manage the timezone for all environments including dev, staging, and production.console$ sudo nano .env
Add the following configuration variable to the file. Replace
America/Aruba
with your desired timezone.phpAPP_TIMEZONE=America/Aruba
Save and close the file.
The above configuration variable sets the default timezone you can reference in your Laravel application. Visit the PHP documentation for a list of timezones you can use in your Laravel project.
Open the
config/app.php
file to enable the timezone in your Laravel application.console$ sudo nano config/app.php
Find the timezone configuration within the
app.php
file.php'timezone' => 'UTC'
Replace the default
UTC
value with your environment variable.php'timezone' => env('APP_TIMEZONE', 'UTC'),
Save and close the file.
The above configuration sets the timezone in your Laravel application by referencing the
APP_TIMEZONE
variable in your.env
configuration. Laravel defaults toUTC
if the variable is missing in your.env
file.Clear the configuration cache to apply the configuration changes.
console$ php artisan config:cache
The above command restarts Laravel to use the updated configuration and timezone in your project. Your output should be similar to the one below when successful.
INFO Configuration cached successfully.
Verify the Active Timezone in Laravel
Follow the steps below to create a new route to test the active timezone in your Laravel project.
Create a new
web.php
file in your routes directory.console$ nano routes/web.php
Add the following route configuration to the
web.php
file.phpRoute::get('/timezone', function () { return config('app.timezone'); });
Save and close the file.
The above configuration creates a new
/timezone
route that displays the active timezone in your Laravel application.Send an HTTP request to the
/timezone
path using your Laravel application's URL and verify that it displays your active timezone. Replacehttp://127.0.0.1:8000
with your actual application URL.console$ curl http://127.0.0.1:8000/timezone
Output:
America/Aruba
Conclusion
You have updated the default timezone and set a new timezone in your Laravel project. Configuring the correct timezone ensures consistent behavior, reliability and accurate date and time handling. You can dynamically set the timezone in Laravel based on the authenticated user's preferences using middleware or service providers in more advanced applications. For more information and advanced configurations, visit the Laravel timezone documentation.
No comments yet.