php function to set time zone: 1. ini_set() function, syntax "ini_set('date.timezone','timezone');"; 2. date_default_timezone_set() function, which can be all times in the script Date functions set a default time zone.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php sets the time zone Function
1. Use the ini_set() function to set the time zone
The ini_set() function in PHP can set the value of the specified configuration option. This Configuration options retain new values while the script is running and are restored when the script ends. The syntax format of the function is as follows:
ini_set($varname, $newvalue)
where $varname
is the configuration option to be modified, and $newvalue
is the new value of the configuration option.
Tips: The ini_set() function cannot modify all configuration options. You can view the configurations that can be modified by visiting "https://www.php.net/manual/zh/ini.list.php" options.
[Example] Use the ini_set() function to set the time zone.
<?php header("Content-type:text/html;charset=utf-8"); ini_set('date.timezone', 'GMT'); echo '当前的格林尼治时间为:'.date('Y-m-d H:i:s',time()).'<br>'; ini_set('date.timezone', 'Asia/Urumqi'); echo '乌鲁木齐的当前时间为:'.date('Y-m-d H:i:s',time()).'<br>'; ini_set('date.timezone', 'Asia/Shanghai'); echo '上海的当前时间为:'.date('Y-m-d H:i:s',time()); ?>
The running results are as follows:
当前的格林尼治时间为:2021-07-05 10:46:25 乌鲁木齐的当前时间为:2021-07-05 18:46:25 上海的当前时间为:2021-07-05 18:46:25
2. Use the date_default_timezone_set() function to set the time zone
The date_default_timezone_set() function in PHP can be used in the script All time and date functions set a default time zone. The syntax format is as follows:
date_default_timezone_set($timezone_identifier)
Parameters $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 or The TZ environment variable also generates E_STRICT level information.
[Example] Use date_default_timezone_set() function to set the time zone.
<?php header("Content-type:text/html;charset=utf-8"); date_default_timezone_set('Asia/Urumqi'); echo '乌鲁木齐的当前时间为:'.date('Y-m-d H:i:s',time()).'<br>'; date_default_timezone_set('Europe/Lisbon'); echo '葡萄牙的当前时间为:'.date('Y-m-d H:i:s',time()).'<br>'; date_default_timezone_set('Asia/Shanghai'); echo '上海的当前时间为:'.date('Y-m-d H:i:s',time()); ?>
The running results are as follows:
乌鲁木齐的当前时间为:2021-07-05 18:47:19 葡萄牙的当前时间为:2021-07-05 11:47:19 上海的当前时间为:2021-07-05 18:47:19
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the functions for setting time zone in php. For more information, please follow other related articles on the PHP Chinese website!