2 Ways to Set Timezone in Laravel 9

July 14, 2022
Laravel
2 Ways to Set Timezone in Laravel 9

In this article, I will share with you how can set timezone in laravel 9 in 2 different ways:

1- Set the timezone manually in laravel
2- Set the timezone dynamically in laravel

By the end of the article, you will know how to set timezone manually and dynamically in laravel 9.

If you are using the laravel 8, then you can check this article on how to set timezone in laravel 8.

Let’s get started!

1. Setting Timezone Manually in laravel

In this part, I will share with you the manual way of setting timezone.

Note:
In this method, if we change the timezone, it will get changed for the entire application.

Step 1: Open Config/app.php file

In this step, first of all, you need to open config/app.php and scroll to timezone variable. You will see that by default the timezone is set to UTC.


    /*
    |--------------------------------------------------------------------------
    | Application Timezone
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default timezone for your application, which
    | will be used by the PHP date and date-time functions. We have gone
    | ahead and set this to a sensible default for you out of the box.
    |
    */

    'timezone' => 'UTC',

Step 2: Change the UTC to your timezone

In this step, you need to replace the UTC with your timezone.

Each timezone has a specific code to it. In case you don’t familiar with your timezone code, you can check the complete list here for the timezone which PHP provides.

For this tutorial, I will be using Europe/Paris as my timezone.

Now the config/app.php the timezone variable will look like this:


    /*
    |--------------------------------------------------------------------------
    | Application Timezone
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default timezone for your application, which
    | will be used by the PHP date and date-time functions. We have gone
    | ahead and set this to a sensible default for you out of the box.
    |
    */

    'timezone' => 'Europe/Paris',

Step 3: Clear the Config Cache

Now you probably, need to clear the config cache in order to reflect the timezone. Run the following command in order to clear the cache:

php artisan config:clear

Step 4: Verify the timezone in the controller or view

In your controller or view, if you type the following code, you will see the current time of the timezone you changed to Paris Time.

$current_date_time=Carbon\Carbon:now();
echo $current_date_time;

That’s all you need to set the timezone manually in laravel 9.

2. Setting Timezone Dynamically in laravel

There are many cases, in which you don’t need to change the entire application timezone but for the specific function itself. Here is how you can do that.

Step 1: Calling the php timezone function

In this part, you need to call the PHP timezone function which  date_default_timezone_set to your controller or view.

date_default_timezone_set('Europe/Paris')
$current_date_time=Carbon\Carbon:now();
echo $current_date_time;

date_default_timezone_set is responsible for setting timezone dynamically for the current request.

Now the timezone will be affected for that controller or view only irrespective of the application timezone.

Step 2: Clear the Cache

If for any reason, the change is not getting affected, you can try clearing the cache itself:

php artisan cache:clear
php artisan config:clear

Conclusion

Hope you have learned today about both ways of setting timezone in laravel 9.

Let me know if you have any queries, see you in the next one.

Write a Reply or Comment

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


Icon