Set Default Time Zone in Laravel 9

Set Default Time Zone in Laravel 9

There might be a case, where we need to set the default time zone in application which will be used by date and time functions. This tutorial provides example how to set default time zone in Laravel 9 application.

Open the .env file, add new environment variable APP_TIMEZONE.

.env

APP_TIMEZONE=America/New_York

In the config/app.php file, find timezone option and replace it with the following line:

config/app.php

<?php

return [
    // Other options ...

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

    // Other options ...
];

Now clear configuration cache using command:

php artisan config:cache

The following code can be used for testing:

Http/Controllers/TestController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Response;

class TestController extends Controller
{
    public function index(): Response
    {
        return new Response(now()->timezone->getName()); // Output: America/New_York
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.