Home>Article>Backend Development> What are the PHP time zone conversion functions?
There are 2 functions: 1. ini_set(), the syntax is "ini_set('date.timezone','identifier)"; 2. date_default_timezone_set(), the syntax is "date_default_timezone_set('identifier')".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
There are two php time zone conversion functions :
ini_set() function
date_default_timezone_set() function
1. Use the ini_set() function to set the time zone
The ini_set() function in PHP can set the time zone by setting the value of the "date.timezone" configuration option.
ini_set('date.timezone', '时区标识符')
There are many time zone identifiers, which can be viewed by visiting "https://www.php.net/manual/zh/timezones.php". Here are some commonly used time zone identifiers And its meaning:
Asia/Shanghai —— Shanghai
Asia/Chongqing —— Chongqing
Asia/Urumqi —— Urumqi
Asia/Hong_Kong —— Hong Kong
Asia/Macao —— Macao
Asia/Taipei - Taipei
Asia/Singapore - Singapore
PRC - China Time Zone
Tip: If you are in China, we can set the time zone to "Asia/Shanghai or PRC"!
[Example] Use the ini_set() function to set the time zone
'; ini_set('date.timezone', 'Asia/Urumqi'); echo '乌鲁木齐的当前时间为:'.date('Y-m-d H:i:s',time()).'
'; ini_set('date.timezone', 'Asia/Shanghai'); echo '上海的当前时间为:'.date('Y-m-d H:i:s',time()); ?>
2. Use the date_default_timezone_set() function to set the time zone
The date_default_timezone_set() function in PHP can set a default time zone for all time and date functions in the script. The syntax format is as follows:
date_default_timezone_set($timezone_identifier)
The parameter $timezone_identifier is the time zone identifier, such as UTC ( Greenwich Mean Time) or Europe/Lisbon (Portugal).
Since PHP5.1.0 (the date and time function has been rewritten in this version), if the time zone is illegal, each call to the date and time function will generate an E_NOTICE level error message. If you use the system Setting the or TZ environment variable will also generate E_STRICT level information.
[Example] Use the date_default_timezone_set() function to set the time zone
'; date_default_timezone_set('Europe/Lisbon'); echo '葡萄牙的当前时间为:'.date('Y-m-d H:i:s',time()).'
'; date_default_timezone_set('Asia/Shanghai'); echo '上海的当前时间为:'.date('Y-m-d H:i:s',time()); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the PHP time zone conversion functions?. For more information, please follow other related articles on the PHP Chinese website!