Learn how to change the timezone in Laravel 10 and keep your application up-to-date with the correct timezone for your users.
As a developer, regardless of whether you are working on a new project or updating the functionality of an already current Laravel program, it could occasionally be necessary to change the application’s timezone and location to coincide with the current timezone in your location.
Laravel comes with the default timezone, which has been set to UTC.
As a developer, regardless of whether you are working on a new project or updating the functionality of an already current Laravel program, it could occasionally be necessary to change the application’s timezone and location to coincide with the current timezone in your location.
Laravel comes with the default timezone, which has been set to UTC.
Read Also: Create Models in Node Js Sequelize ORM (updated 2024)
Changing The Timezone in Laravel
There are two primary methods to change the timezone.
Option 1:
From the root directory of your project, open the config
directory.
Edit the app.php
file next and then change the timezone
value will change from UTC and select the desired timezone from the menu of timezones available.
// Change only the timezone
'timezone' => 'America/New_York',
Option 2:
Modify the .env
file to define the timezone in the following manner:
APP_TIMEZONE='America/New_York'
Then, include the following information in the app.php
file:
'timezone' => env('APP_TIMEZONE', 'UTC'),
// Second parameter, UTC is set to be the default value for timezone.
Run the following command in the terminal to clear the cache:
php artisan config:clear
Install Laravel
Creating the Laravel app is not necessary for this step, but if you haven’t done it yet, you can proceed by executing the following command
composer create-project laravel/laravel Change-Timezone
Copy
Feel free to ignore this step if you’ve already created the Laravel app.
Example 1: using setTimezone()
At this stage, we need to develop a new controller called TimeZoneController. This controller will have a method: index(). Let’s update the following code to your controller file
app/Http/Controllers/TimeZoneController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
class TimeZoneController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$time = Carbon::now()->setTimezone("Asia/Dhaka");
dd($time);
}
}
Copy
Output
2023-07-22 20:09:24.915678 Asia/Dhaka (+06:00)
Copy
Example 2: using tz()
This controller will have a method: index(). Let’s update following code to your controller file
app/Http/Controllers/TimeZoneController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
class TimeZoneController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$time = Carbon::createFromFormat('Y-m-d H:i:s', '2023-05-23 05:01:01')->tz("Asia/Dhaka");
dd($time);
}
}
Copy
Output
2023-05-23 11:01:01.0 Asia/Dhaka (+06:00)
Copy
How to set Timezone in Laravel
There are 3 ways to set a timezone in the Laravel 10, 9, and 8 applications. 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 the 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’
Read Also: How To Trim Whitespace from a String in Python: Easy Methods
Set Timezone in .env File
In the second method, open the .env file, which is located in the root directory.
If you want to change the timezone to Asia timezone in Laravel. Then add the APP_TIMEZONE variable in the .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:
In conclusion, updating the timezone in a Laravel 10 application is a simple process that can greatly improve user experience. By following the methods outlined in this article, you can easily change the timezone and ensure that your application displays the correct time for your users, regardless of their location. Keeping your application up-to-date with the correct timezone is an important aspect of maintaining a successful Laravel 10 application.
Thank you for reading this article.