How to set or get default timezone in PHP
Today we are going to explain to you how to set or get a default timezone in PHP. If you are looking to change the default timezone for all date and time in PHP then here you will find the solution.
Using php built-in function date_default_timezone_set() and date_default_timezone_get() we can set and get default timezone respectively.
You can also find the similar articles about the Time Zone and DateTime.
Timezone in PHP
1. Set default timezone in PHP
To set the default timezone, we need to edit the php script and add below line at top of the script.
Example
1 2 3 4 5 6 | <?php date_default_timezone_set("America/New_York"); echo date('H:i:s'); ?> // Output: 12:43:47 |
Here, we have printed the date after setting the timezone so we can check that we are getting the correct time as we have set the timezone.
2. Get default timezone in PHP
You can get the current timezone using date_default_timezone_get()
function anywhere in php script.
Example
1 2 3 4 5 | <?php echo date_default_timezone_get(); ?> // Output: America/New_York |
Here you will find the list of the available timezone in PHP.
That’s it for today.
Thank you for reading. Happy Coding..!!