To set timezone in Laravel; In this tutorial, you will learn how to dynamically and globally set timeZone in Laravel 8, 9, 10 applications using .env, app.php file.
How to set Timezone in Laravel
There are 3 ways to set a timezone in the Laravel 10, 9, 8 application. which are given below:
- Set Timezone in App.php File
- Set Timezone in .env File
- Set Timezone Dynamically
Set Timezone in App.php File
In this method, you have to open the app.php file. Which is located inside config directory.
By default timezone is set as UTC, if you want to change it to America timezone, you can do as follows:
'timezone' => 'America/New_York'
Set Timezone in .env File
In the second method, open .env file, which is located in root directory.
If you want to change timezone to Asia timezone in laravel. Then add APP_TIMEZONE variable in .env file, you can do as follows:
APP_TIMEZONE='Asia/Tokyo'
And you can add the below code in your app.php file.
'timezone' => env('APP_TIMEZONE', 'UTC'),
Set Timezone Dynamically
Using the third method, you can set dynamically timezone with date_default_timezone_set(), you can do as follows:
public function index(Request $request) { date_default_timezone_set('America/Los_Angeles'); $now = Carbon::now(); dd($now); }
Certainly! Here is a list of all timezones supported by PHP Laravel that you can use in your Laravel configuration file:
'timezone' => 'Europe/Paris': Paris, France 'timezone' => 'America/New_York': New York, USA 'timezone' => 'Asia/Tokyo': Tokyo, Japan 'timezone' => 'Australia/Sydney': Sydney, Australia 'timezone' => 'Africa/Johannesburg': Johannesburg, South Africa 'timezone' => 'America/Sao_Paulo': São Paulo, Brazil 'timezone' => 'Asia/Dubai': Dubai, United Arab Emirates 'timezone' => 'Pacific/Auckland': Auckland, New Zealand 'timezone' => 'America/Mexico_City': Mexico City, Mexico 'timezone' => 'Europe/Moscow': Moscow, Russia
Conclusion
That’s it; In this tutorial, you have learned how to set timeZone in Laravel 8, 9, 10 applications.
Recommended Tutorials