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
.
APP_TIMEZONE=America/New_York
In the config/app.php
file, find timezone
option and replace it with the following line:
<?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:
<?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