Home >Backend Development >PHP Problem >What are the functions for setting time zone in php

What are the functions for setting time zone in php

青灯夜游
青灯夜游Original
2021-07-05 18:46:302625browse

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.

What are the functions for setting time zone in php

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(&#39;date.timezone&#39;, &#39;GMT&#39;);
echo &#39;当前的格林尼治时间为:&#39;.date(&#39;Y-m-d H:i:s&#39;,time()).&#39;<br>&#39;;
ini_set(&#39;date.timezone&#39;, &#39;Asia/Urumqi&#39;);
echo &#39;乌鲁木齐的当前时间为:&#39;.date(&#39;Y-m-d H:i:s&#39;,time()).&#39;<br>&#39;;
ini_set(&#39;date.timezone&#39;, &#39;Asia/Shanghai&#39;);
echo &#39;上海的当前时间为:&#39;.date(&#39;Y-m-d H:i:s&#39;,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(&#39;Asia/Urumqi&#39;);
echo &#39;乌鲁木齐的当前时间为:&#39;.date(&#39;Y-m-d H:i:s&#39;,time()).&#39;<br>&#39;;
date_default_timezone_set(&#39;Europe/Lisbon&#39;);
echo &#39;葡萄牙的当前时间为:&#39;.date(&#39;Y-m-d H:i:s&#39;,time()).&#39;<br>&#39;;
date_default_timezone_set(&#39;Asia/Shanghai&#39;);
echo &#39;上海的当前时间为:&#39;.date(&#39;Y-m-d H:i:s&#39;,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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn