Laravel Set Timezone Dynamically

December 2, 2021
Laravel
Laravel Set Timezone Dynamically

In this blog post, you will learn how to set the timezone dynamically in laravel. When we are dealing with users from different timezones, you probably need somewhere their timezones.

By the end of the article, you will be able to perform the timezone dynamically in laravel.

Let’s get started!

Step 1: Calling the PHP change timezone function

In this step, we just need to call the PHP change timezone function date_default_timezone_set in the controller or view. In the argument, we need to pass the timezone of the place which you want to set.

date_default_timezone_set('Europe/Paris')

Here Europe/Paris is the timezone place which I am setting.

If you don’t know the timezone of the place which you want to set, follow this link https://www.php.net/manual/en/timezones.php which will show all the timezones.

Step 2: Get the date and time

Now we just need to print the date and time itself. I am using Carbon library to get the current date and time.

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

That’s it, it will give us the time and date of the timezone we set.

Conclusion:

Hope you have learned today how to set the timezone dynamically in laravel.

If you have any questions or queries, please write in the comments box below.

With this, I will say bye for now but of course you in the next one.

Keep learning and keep working, you are a good programmer I know.

Write a Reply or Comment

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


Icon