Home  >  Article  >  Backend Development  >  Time zone setting date_default_timezone_set()

Time zone setting date_default_timezone_set()

炎欲天舞
炎欲天舞Original
2018-05-31 10:14:002512browse


date_default_timezone_set() sets the time zone

1
2009-05-15 02:20:42 1
2009-05-15 02:20:42 1
2009-05-14 18:20:42 1
2009-05-15 10:20:42 1
2009-05-15 10:20:42

There was a remaining problem before, which is the time returned by echo date("y-m-d h:i:s",time()) It always fails to match 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.ini according to the following tips and could not find the date.timezone line. It is There's nothing you can do about it, of course not, haha, if you don't have it, just add it yourself, and make 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-_-
----------------------------------------- -------------------------------------------------- --------------------------
Solution:
Use date_default_timezone_set() in the header to set my default time zone It is Beijing time
date_default_timezone_set('PRC');
echo date('Y-m-d H:i:s');

The time is the same as the current time of the server!! Congratulations
Attachment The usage of date_default_timezone_set is as follows
--------------------------------
date_default_timezone_set
(PHP 5 > = 5.1.0RC1)
date_default_timezone_set -- Set the default time zone for all date and time functions in a script
Description
bool date_default_timezone_set (string timezone_identifier)
date_default_timezone_set() Set for all dates The default time zone for time functions.
Note: Since PHP 5.1.0 (the date and time functions have 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.
Parameters
timezone_identifier
Time zone identifier, such as UTC or Europe/Lisbon
Return value
This function always returns TRUE (even if the timezone_identifier parameter is illegal).
-------------------------------------------------- --------------------------------------------------
Attach the time zone Identifier:
CET
CST6CDT
Cuba
EET
Egypt
Eire
EST
EST5EDT
Etc/GMT
Etc/GMT+0
Etc/GMT+1
Etc/GMT+10
Etc/GMT+11
Etc/GMT+12
Etc/GMT+2
Etc/GMT+3
Etc/GMT+4
Etc/GMT+5
Etc/GMT+6
Etc/GMT+7
Etc/GMT+8
Etc/GMT+9
Etc/ GMT-0
Etc/GMT-1
Etc/GMT-10
Etc/GMT-11
Etc/GMT-12
Etc/GMT-13
Etc/GMT- 14
Etc/GMT-2
Etc/GMT-3
Etc/GMT-4
Etc/GMT-5
Etc/GMT-6
Etc/GMT-7
Etc/GMT-8
Etc/GMT-9
Etc/GMT0
Etc/Greenwich
Etc/UCT
Etc/Universal
Etc/UTC
Etc/ Zulu
Factory
GB
GB-Eire
GMT
GMT+0
GMT-0
GMT0
Greenwich
Hongkong
HST
Iceland
Iran
Israel
Jamaica
Japan
Kwajalein
Libya
MET
MST
MST7MDT
Navajo
NZ
NZ- CHAT
Poland
Portugal
PRC
PST8PDT
ROC
ROK
Singapore
Turkey
UCT
Universal
UTC
W- SU
WET

The above is the detailed content of Time zone setting date_default_timezone_set(). 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