Set default timezone in PHP | date_default_timezone_set()
Do you need to change the default timezone for all the date and time functions of PHP? If you are looking for something for this, then you are at the place where you will get the solution. In this article, I will tell you how to set default timezone in PHP.
In many PHP web development projects, you may need to allow the user to choose timezone or use different timezone in different locations. So this article may helpful for many developers.
Now, let’s see how we can perform this task…
PHP has an inbuilt function date_default_timezone_set(). Using this function, we can set the timezone. So this function will be able to help in our task.
So let’s see our code:
<?php date_default_timezone_set("Asia/Kolkata"); echo date_default_timezone_get(); echo "<br>"; echo date('H:i:s'); ?>
In the above code, we have set the timezone for “Asia/Kolkata” and then use the date_default_timezone_get() to return the current timezone. The date_default_timezone_get() is also a inbuilt PHP function that return the default timezone.
To check if the timezone giving the correct time of that timezone, here we have used the PHP date() function. Well, the default timezone we set will be applied in all the date and time function of PHP.
If we run the code, we will be able to see the timezone and the current time for the timezone like you can see below:
Now if we change the timezone, we will see the changing of timezone as well the current time given from the PHP date() function. You can check all the available timezone from the official PHP time zone listing page.