How to set Timezone in Laravel

To set or change timezone manually, dynamically, and globally in Laravel apps, you can edit the app.php or .env file and add timezone value from UTC to the desired timezone to make changes. and as well as, you can change set default timezone in laravel projects.

Here are 3 options to set or change a timezone in the Laravel 11, 10, 9, and 8 applications manually, dynamically, and globally:

  • Option 1: Set Timezone in App.php File
  • Option 2: Set Timezone in .env File
  • Option 3: Laravel Set Timezone Dynamically

Option 1: Set Timezone in App.php File

Navigate to config directory and open the app.php file, and add 'timezone' => 'America/New_York' variable in it to set or change timezone value in laravel;

'timezone' => 'America/New_York'

To get timezone from app.php file, just simply use \Config::get('app.timezone') on model, views and controller in larave projects:

\Config::get('app.timezone')

Option 2: Set Timezone in .env File

Navigate to laravel project root directory and open the .env file, after that, add APP_TIMEZONE variable to change timezone to Asia timezone; as follows:

APP_TIMEZONE='Asia/Tokyo'

Next, you can add the below code in your app.php file.

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

To get timezone from env file, just simply use env('APP_TIMEZONE') on model, views and controller in larave projects:

env('APP_TIMEZONE');

Option 3: Laravel Set Timezone Dynamically

To set or change timezone dynamically in Laravel, you can use date_default_timezone_set() anywhere in Laravel project like model, controller, view, etc; 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 and change default timezone in laravel Laravel 11, 10, and 9 applications.

Recommended Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *