English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP Date & Time Function Manual
The date_default_timezone_set() function sets the default time zone for all date and time functions in a script
date_default_timezone_set()The function is used to set the default time zone for all functions in the script.
date_default_timezone_set(timezone)
Serial number | Parameters and descriptions |
---|---|
1 | timezone (required) The string that needs to be set as the default time zone. |
PHP date_default_timezone_set() function returns a boolean value, if the given time zone string is valid, thentrue,otherwisefalse.
This function was originally introduced in PHP version5.1introduced in version 5.2.0 and can be used in all higher versions.
The following example demonstratesdate_default_timezone_setfunction usage-
<?php //Set time zone $tz = 'Asia/Shanghai'; date_default_timezone_set($tz); $timeZone = date_default_timezone_get(); print('Default time zone: '. $timeZone); ?>Test and see‹/›
Output result
Default time zone: Asia/Shanghai
The following example compares the default time zone and ini-Set time zone.-
<?php //Set time zone $tz = 'Asia/Shanghai'; date_default_timezone_set($tz); //Retrieve the default time zone $timeZone = date_default_timezone_get(); print('Default time zone: '. $timeZone); print('\n'); //Compare the time zone with the time zone set in the ini configuration if (strcmp($timeZone, ini_get('date.timezone'))){ print('The script time zone is different from the ini configuration set time zone'); } else { print('The script time zone is the same as the ini configuration set time zone'); } ?>Test and see‹/›
Output result
Default time zone: Asia/Shanghai The script time zone is different from the ini configuration set time zone
<?php $dateSrc = '2007-04-19 12:50 GMT'; $dateTime = date_create($dateSrc);; $DateTimeZone = date_timezone_get($dateTime); echo 'Return time zone is '. timezone_name_get($DateTimeZone); echo '\n'; # Use the second function. $dateTime = new DateTime($dateSrc); $DateTimeZone = $dateTime;-getTimezone(); echo 'Return time zone is '. timezone_name_get($DateTimeZone); ?>Test and see‹/›
Output result:
Return time zone is GMT Return time zone is GMT