English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP Date & Time Funktionshandbuch
Die Funktion date_default_timezone_set() setzt die Standardzeitzone für alle Datums- und Zeitfunktionen in einem Skript.
date_default_timezone_set()Die Funktion dient dazu, die Standardzeitzone für alle Funktionen in einem Skript zu setzen.
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. |
The 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_setUsage of functions-
<?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 the 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 and the time zone set in the ini configuration are different'); } else { print('The script time zone and the time zone set in the ini configuration are the same'); } ?>Test and see‹/›
Output result
Default time zone: Asia/Shanghai The script time zone and the time zone set in the ini configuration are different
<?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