How to Set the Timezone in Laravel

Laravel is a robust, object-oriented PHP framework designed to build modern, scalable web applications — from simple sites to complex enterprise-level solutions. It follows the Model-View-Controller (MVC) architecture, helping you create maintainable and clean code with its elegant PHP syntax.

This guide explains how to configure the timezone in Laravel. Setting the correct timezone ensures consistent handling of dates and times in your application, especially for databases, logs, and frontend timestamps.

Prerequisites

Before starting, make sure you meet the following requirements:

  • Access to an Ubuntu 24.04 instance with an active Laravel project.

Laravel Timezone Formats

Laravel includes built-in timezone management, allowing accurate date and time operations across different regions. Laravel handles timezones primarily through two approaches:

  • Application Timezone: Laravel stores a global default timezone in the config/app.php file. This setting affects components such as Eloquent, Carbon, and scheduled tasks.
  • User Timezone: Each user’s timezone can be stored in the database or determined by their browser preferences. Displaying local timezones for users enhances usability, especially in global applications.

Set the Timezone in Laravel

By default, Laravel uses the UTC timezone. Follow these steps to update the timezone configuration in your Laravel project.

Step 1: Navigate to Your Laravel Project Directory

Step 2: Open the .env File

Add the following configuration variable. Replace America/Aruba with your preferred timezone.

APP_TIMEZONE=America/Aruba

Save and close the file.

This configuration defines the default timezone your Laravel app will reference. Check the official PHP documentation for a full list of supported timezones.

Step 3: Edit the config/app.php File

Open the Laravel application configuration file to apply the timezone setting.

$ sudo nano config/app.php

Step 4: Locate and Modify the Timezone Setting

Find the existing timezone configuration:

Replace it with the environment variable reference:

'timezone' => env('APP_TIMEZONE', 'UTC'),

Save and close the file.

This update configures Laravel to use the timezone value from the APP_TIMEZONE variable. If it’s not set in your .env file, Laravel will default to UTC.

Step 5: Clear the Configuration Cache

Apply the changes by clearing the configuration cache.

$ php artisan config:cache

After running the command, Laravel reloads the configuration with your new timezone settings.

Expected output:

INFO  Configuration cached successfully.

Verify the Active Timezone in Laravel

To confirm that the timezone update is active, you can create a simple route that displays the current timezone.

Step 1: Create the web.php File

Step 2: Add the Route Configuration

Route::get('/timezone', function () {
    return config('app.timezone');
});

Save and close the file.

This route returns the active timezone configured in your Laravel application.

Step 3: Test the Timezone Route

Use a curl command or your browser to check the /timezone endpoint. Replace http://127.0.0.1:8000 with your application’s actual URL.

$ curl http://127.0.0.1:8000/timezone

Example output:

Conclusion

You have successfully updated the default timezone in your Laravel application. Configuring the proper timezone ensures consistent and accurate date and time handling across your application. For advanced configurations, you can dynamically set the timezone using middleware or service providers based on user preferences. Refer to the official Laravel documentation for more details on timezone configuration.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: