What is PHP date_default_timezone_set() Function?
To set the default timezone for your script, use PHP date_default_timezone_set() function. This function is useful when your web application requires to run in a different timezone. After setting a timezone with this function, all the date/time functions in that script will use this as the timezone.
By default, every PHP script uses server’s timezone which you find in “date.timezone” settings in php.ini file. This function overrides the server’s timezone with its timezone.
Syntax:
date_default_timezone_set(timezone)
Parameters:
Timezone (Required): The name of a timezone. The following is a partial list of the supported timezones in the world according to their region. You’ll get the full list in the “List of supported timezones”. If you’re unsure of your desire timezone, use this “Time Zone Map” to select one.
Africa (Region) Africa/Cairo Africa/Johannesburg Africa/Casablanca … America (Region) America/New_York America/Los_Angeles America/Argentina/San_Juan … Antarctica (Region) Antarctica/Casey Antarctica/Palmer Antarctica/Davis … Arctic (Region) Arctic/Longyearbyen | Asia Asia/Tokyo Asia/Qatar Asia/Shanghai … Atlantic Atlantic/Stanley Atlantic/South_Georgia Atlantic/Faroe … Australia Australia/Sydney Australia/Melbourne Australia/Brisbane … Europe Europe/London Europe/Moscow Europe/Paris … | Indian Indian/Mauritius Indian/Cocos Indian/Maldives … Pacific Pacific/Honolulu Pacific/Auckland Pacific/Fiji … Others America/Montreal Australia/West Etc/GMT |
Return Values:
The function returns –
- TRUE – if the timezone identifier is a valid one.
- Error notice – if the timezone identifier is not a valid one.
Examples:
Example 1:
<?php
date_default_timezone_set("America/New_York");
echo "Current timezone is: " . date_default_timezone_get();
?>
Output:
Current timezone is: America/New_York
Explanation:
In line 2, we set the default timezone as “America/New_York”. In the next line we displays the current timezone by the date_default_timezone_get() function.
Example 2:
<?php
date_default_timezone_set("America/New_York");
echo "Local New York time is: " . date("m-j-Y g:i:s a");
echo "<br />";
date_default_timezone_set("Asia/Hong_Kong");
echo "Local Hong Kong time is: " . date("m-j-Y g:i:s a");
?>
Output:
Local New York time is: 12-28-2023 1:45:46 am
Local Hong Kong time is: 12-28-2023 2:45:46 pm
Explanation:
In line 2, we set the timezone as “America/New_York”. So, the date() function in the next line displays New York’s date and time. In line 5, we set the timezone as “Asia/Hong_Kong”. So, the date() function in the next line displays Hong Kong’s date and time.
Notes on date_default_timezone_set() Function:
If you want to safely and consistently use same timezone in all script in your web application, change your server’s timezone in php.ini file instead of using this function if you have access into php.ini file.
PHP Version Support:
PHP 5 >= 5.1.0, PHP 7, PHP 8
Summary: PHP date_default_timezone_set() Function
date_default_timezone_set() function is a built-in PHP date/time function. The date_default_timezone_set() function ensures that your web application running in the required timezone.
Reference:
https://www.php.net/manual/en/function.date-default-timezone-set.php