PHP date_ default_ timezone_ Example analysis of set() setting time zone operation

Time:2020-10-16

This paper describes PHP date as an example_ default_ timezone_ Set() sets the time zone operation. For your reference, the details are as follows:

<?php
echo function_ exists(date_ default_ timezone_ Set). "< br >"; // here, he always returns 1, which is used to judge whether the character in this is a defined function name
Echo date ('y-m-d H: I: s'). "< br >"; // time of default time zone

echo date_ default_ timezone_ Set ("etc / GMT"). "< br >"; // this is Greenwich mean time, and the time obtained is the same as the default time zone
echo date('Y-m-d H:i:s')."<br>";

echo date_ default_ timezone_ Set ("etc / GMT + 8")
echo date('Y-m-d H:i:s')."<br>";

echo date_ default_ timezone_ Set ("etc / GMT-8"). "< br >"; // as can be imagined from the previous one, we are 8 hours faster than that, so we reduce 8
echo date('Y-m-d H:i:s')."<br>";

echo date_ default_ timezone_ Set ('prc '). "< br >"; // sets the Chinese time zone
Echo date ('y-m-d H: I: s'). "< br >"; // China standard time
?>

output

1
2009-05-15 02:20:42 / / default time zone
1
2009-05-15 02:20:42 / (“etc / GMT”) Greenwich mean time
1
2009-05-14 18:20:42 / (“etc / GMT + 8”) is 8 hours slower than linwich standard time
1
2009-05-15 10:20:42 / (“etc / GMT-8”) is 8 hours faster than Lin Weizhi standard time, which is our Beijing time
1
2009-05-15 10:20:42 / (‘prc ‘) China standard time

There was a problem left over beforeecho date("y-m-d h:i:s",time())The time of coming back is always not in line with the actual time. Today, I finally found the reason and solution on the Internet. I share the following:

I’ll add that I’ll follow the instructions below php.ini can’t find date.timezone This line, is there no way, of course not, ha ha, if not, you can add it by yourself and enrich yourself. I added this sentence date.timezone =”PRC”, the problem is solved, happy

From php5.1.0, php.ini Added in date.timezone This option is off by default

That is, the displayed time (regardless of the PHP command) is Greenwich mean time

It’s just 8 hours different from our time (Beijing time). There are three ways to return to normal time.

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

2. If it must be used and cannot be modified php.ini In the statement about time initialization
Add date to it_ default_ timezone_ set (XXX);
CP has no such problem

3. Once and for all, it can only be modified php.ini 。 open php.ini lookup date.timezone Remove the semicolon from the front
=Add XXX after it and restart the HTTP service (such as Apache 2 or IIS)

For 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 / Macau, Asia / Hong_ Hong Kong, Asia / Taipei (Macao, Hong Kong, Taipei in turn)
And Singapore: Asia / Singapore
It seems that foreigners have missed Beijing
Other available values are: etc / GMT-8, Singapore, Hong Kong, PRC
What is PRC? PRC is the people’s Republic of China-_-
———————————————————————————————————————

Solution:

Use date in the header_ default_ timezone_ Set() sets my default time zone to Beijing time


date_default_timezone_set('PRC');
echo date('Y-m-d H:i:s');

The time is the same as the server’s current time! Congratulations

Date attached_ default_ timezone_ Set is used as follows

——————————–
date_default_timezone_set

(PHP 5 >= 5.1.0RC1)
date_ default_ timezone_ Set — sets the default time zone for all date time functions in a script
explain
bool date_default_timezone_set ( string timezone_identifier )

date_ default_ timezone_ Set() sets the default time zone for all date time functions.

Note: since PHP 5.1.0 (the date time function is rewritten in this version), if the time zone is out of order, each call to the date time function will generate an E_ Error messages at the note level.

parameter

timezone_identifier

Time zone identifier, such as UTC or Europe / Lisbon

Return value
This function always returns true (even timezone_ The 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

PS: here are some time and date related tools for your reference

Online date / day Calculator:
http://tools.jb51.net/jisuanqi/date_jisuanqi

Online date calculator / difference days Calculator:
http://tools.jb51.net/jisuanqi/datecalc

Online date day difference calculator:
http://tools.jb51.net/jisuanqi/onlinedatejsq

UNIX timestamp conversion tool:
http://tools.jb51.net/code/unixtime

More about PHP related content interested readers can see the special topic: PHP date and time usage summary, PHP array (array) operation skills encyclopedia, PHP basic syntax introduction course, PHP operation and operator usage summary, PHP object-oriented program design introduction tutorial, PHP string usage summary, PHP + MySQL database operation into Door tutorial “and” PHP common database operation skills summary “

I hope this article will help you with PHP programming.