Home >Backend Development >PHP Tutorial >date_default_timezone_set() method to set time zone in PHP

date_default_timezone_set() method to set time zone in PHP

一个新手
一个新手Original
2017-09-12 10:49:241901browse

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>";//中国标准时间
?>
//输出
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;) 中国标准时间

There was a legacy problem before, that is, the time returned by echo date("y-m-d h:i:s", time()) always did not match the actual time. Today I finally found the reason and solution online. Share as follows:

I would like to add that I modified php.ini according to the following tips and could not find the date.timezone line. Is there nothing I can do? Of course not, haha, if not, just add it yourself. Well, you can make enough food and clothing by yourself. I added date.timezone = "PRC" and the problem was solved. I'm happy.

Starting from php5.1.0, the date.timezone option has been added to php.ini. It is turned off by default. The

means that the displayed time (no matter what php command is used) is Greenwich Mean Time.

is exactly 8 hours different from our time (Beijing time). There are the following 3 Method can restore normal time.

1, the simplest way is not to use php5.1 or above version

2, if you must use it, and you cannot modify php.ini, you need to initialize the time statement
Add 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 ​​in 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 date_default_timezone_set() method to set 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
Previous article:Laravel installation tipsNext article:Laravel installation tips