How to Set the Timezone in Laravel

Updated on 24 April, 2025
How to Set the Timezone in Laravel header image

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:

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.

  1. Navigate to your Laravel project's root directory such as /var/www/laravelapp.

    console
    $ cd /var/www/laravelapp
    
  2. Open the .env file to manage the timezone for all environments including dev, staging, and production.

    console
    $ sudo nano .env
    
  3. Add the following configuration variable to the file. Replace America/Aruba with your desired timezone.

    php
    APP_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.

  4. Open the config/app.php file to enable the timezone in your Laravel application.

    console
    $ sudo nano config/app.php
    
  5. Find the timezone configuration within the app.php file.

    php
    'timezone' => 'UTC'
    
  6. 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 to UTC if the variable is missing in your .env file.

  7. 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.

  1. Create a new web.php file in your routes directory.

    console
    $ nano routes/web.php
    
  2. Add the following route configuration to the web.php file.

    php
    Route::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.

  3. Send an HTTP request to the /timezone path using your Laravel application's URL and verify that it displays your active timezone. Replace http://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.

Tags:

Comments

No comments yet.