DEV Community

Cover image for How to Change Timezone in Laravel with Example
saim
saim

Posted on • Originally published at larainfo.com

How to Change Timezone in Laravel with Example

In this section we will see how to change timezone in Laravel. To change the time zone in a Laravel application, you need to modify the config/app.php file. Here's a step-by-step guide on how to do it.

Laravel 10 Find Month Difference Between Two Dates with Example

1.Locate the config/app.php File: This file is located in the config directory of your Laravel application.

2.Find the Timezone Setting: Inside this file, look for a line that specifies the timezone key. It should look something like this.

'timezone' => 'UTC',
Enter fullscreen mode Exit fullscreen mode

By default, Laravel is set to UTC.
laravel change timezone utc

3.Change the Timezone: Replace UTC with the timezone you want to use. Time zones are generally specified as strings like America/New_York, Asia/Tokyo, etc. For example, to set the timezone to Eastern Standard Time (EST), you would change the line to.

'timezone' => 'America/New_York',
Enter fullscreen mode Exit fullscreen mode

Save the file After making your changes, save the file.
4.Clear Configuration Cache (Optional): If your application is in production, you may need to clear the configuration cache for the changes to take effect. You can do this by running the following command in your terminal.

php artisan config:clear
Enter fullscreen mode Exit fullscreen mode

Local Time for Order Processing in Warehouses Worldwide

In Laravel, when an order is processed, the system checks the warehouse's location and uses its local time. For instance, if an order is processed in the Tokyo warehouse, Laravel sets the timestamp to the Asia/Tokyo timezone for that operation.

$warehouseTimezone = 'Asia/Tokyo'; // This would be dynamically determined based on the warehouse location
$orderProcessedTime = Carbon::now($warehouseTimezone)->format('Y-m-d H:i:s');
Enter fullscreen mode Exit fullscreen mode

Displaying Order Times to Users

When users check their order history, they see the processing times in their own timezone. For a user in Berlin, the system changes the warehouse processing time from the warehouse's time to Europe/Berlin.

$userTimezone = 'Europe/Berlin'; // Assume this is the user's selected or detected timezone
$orderProcessedTime = $order->processed_at->timezone($userTimezone)->format('Y-m-d H:i:s');
Enter fullscreen mode Exit fullscreen mode

Top comments (0)