Use date_default_timezone_set() to set the time zone method

一个新手
Release: 2023-03-16 13:16:02
Original
1514 people have browsed it

date_default_timezone_set() sets the time zone

<?php
echo function_exists(date_default_timezone_set)."<br>";//在这他总是返回1,这函数是判断这里面的字符是不是一个定义了的函数名
echo date(&#39;Y-m-d H:i:s&#39;)."<br>";//默认时区的时间
echo date_default_timezone_set("Etc/GMT")."<br>";//这是格林威治标准时间,得到的时间和默认时区是一样的
echo date(&#39;Y-m-d H:i:s&#39;)."<br>";
echo date_default_timezone_set("Etc/GMT+8")."<br>";//这里比林威治标准时间慢8小时
echo date(&#39;Y-m-d H:i:s&#39;)."<br>";
echo date_default_timezone_set("Etc/GMT-8")."<br>";//由上一个不能难想像,我们比那快8小时所以减8
echo date(&#39;Y-m-d H:i:s&#39;)."<br>";
echo date_default_timezone_set(&#39;PRC&#39;)."<br>"; //设置中国时区
echo date(&#39;Y-m-d H:i:s&#39;)."<br>";//中国标准时间
?>
Copy after login
//输出
1
2009-05-15 02:20:42 //默认时区时间
1
2009-05-15 02:20:42 //("Etc/GMT") 格林威治标准时间
1
2009-05-14 18:20:42 //("Etc/GMT+8") 比林威治标准时间慢8小时
1
2009-05-15 10:20:42 //("Etc/GMT-8") 比林威治标准时间快8小时,是我们的北京时间
1
2009-05-15 10:20:42 //(&#39;PRC&#39;) 中国标准时间
Copy after login

There was a remaining problem before, which was echo date("y-m-d h: The time returned by i:s", time()) is always inconsistent with the actual time. Today I finally found the reason and solution online. I share it as follows:
I would like to add that I modified PHP according to the method suggested below. .ini can't find the date.timezone line. Is there nothing I can do about it? Of course not. Haha, if you don't have it, just add it yourself. You can have enough food and clothing by yourself. I added date.timezone = "PRC" and the problem was solved. I am happy.
Starting from php5.1.0, the date.timezone option has been added to php.ini, which is turned off by default
That is, the displayed time (no matter what php command is used) is Greenwich Mean Time
It is exactly 8 hours different from our time (Beijing time). There are three methods below to restore the normal time.
1. The simplest way is not to use php5.1 or above.
2. If you must use it and cannot modify php.ini, you need to add
above the statement about time initialization. Date_default_timezone_set (XXX);
cp does not have this problem
3, once and for all, only php.ini can be modified. Open php.ini and search for date.timezone. Remove the semicolon in front
= and add XXX after it. Restart the http service (such as apache2 or iis, etc.)
Regarding XXX, the available values ​​​​for mainland China are: Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (in order Chongqing, Shanghai, Urumqi)
Available in Hong Kong and Taiwan: Asia/Macao, Asia/Hong_Kong, Asia/Taipei (in order Macau, Hong Kong, Taipei)
and Singapore :Asia/Singapore
Foreigners seem to have missed Beijing
Other available values ​​are: Etc/GMT-8, Singapore, Hongkong, PRC
What is PRC? PRC is the People's Republic of China -_-

The above is the detailed content of Use date_default_timezone_set() to set the time zone method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template