SoFunction
Updated on 2025-03-03

Analysis of PHP date_default_timezone_set time zone operation instance

This article describes the operation of setting time zones of PHP date_default_timezone_set(). Share it for your reference, as follows:

<?php
echo function_exists(date_default_timezone_set)."<br>";//In this, it always returns 1. This function determines whether the character here is a defined function name.echo date('Y-m-d H:i:s')."<br>";//Default time zone time
echo date_default_timezone_set("Etc/GMT")."<br>";//This is Greenwich Standard Time, the time obtained is the same as the default time zoneecho date('Y-m-d H:i:s')."<br>";

echo date_default_timezone_set("Etc/GMT+8")."<br>";// Here is 8 hours slower than Linwich Standard Timeecho date('Y-m-d H:i:s')."<br>";

echo date_default_timezone_set("Etc/GMT-8")."<br>";//It is not difficult to imagine from the previous one, we are 8 hours faster than that so we cut 8echo date('Y-m-d H:i:s')."<br>";

echo date_default_timezone_set('PRC')."<br>"; //Set the China Time Zoneecho date('Y-m-d H:i:s')."<br>";//China Standard Time?>

Output

1
2009-05-15 02:20:42 //Default time zone time
1
2009-05-15 02:20:42 //("Etc/GMT") Greenwich Standard Time
1
2009-05-14 18:20:42 //("Etc/GMT+8") 8 hours slower than Linwich Standard Time
1
2009-05-15 10:20:42 //("Etc/GMT-8") 8 hours faster than Linwich Standard Time, our Beijing time
1
2009-05-15 10:20:42 //('PRC') China Standard Time

There was a legacy problem before, that isecho date("y-m-d h:i:s",time())The time I came back always doesn’t match the actual time. Today, I finally found the reason and solution online. I share it as follows:

I will add that I can’t find this line by following the method prompted below. Isn’t there any way? Of course not. Haha, if not, just add it yourself and do it yourself to make enough food and clothing. I added this sentence = "PRC", and the problem was solved, so I'm happy.

Starting from php5.1.0, this option has been added, which is turned off by default

That is, the time displayed (regardless of the php command) is Greenwich Standard Time

It is exactly 8 hours apart from our time (Beijing time). There are three methods below to restore normal time.

1. The easiest way is not to use php5.1 or above

2. If it is necessary to use and cannot be modified, it needs to be initialized in the statement about time.
Add date_default_timezone_set (XXX);
There is no problem with cp

3. Once and for all, only to be modified. Open Search Remove the previous semicolon
= Add XXX afterwards, restart http service (such as apache2 or iis, etc.)

Regarding XXX, the available values ​​in mainland China are: Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (Chongqing, Shanghai, Urumqi in turn)
Available in * and *: Asia/Macao, Asia/Hong_Kong, Asia/Taipei (Macau, *, Taipei in turn)
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 *-_-
---------------------------------------------------------------------------------------------------------------------

Solution:

Use date_default_timezone_set() to set my default time zone to Beijing time at the header

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

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
illustrate
bool date_default_timezone_set ( string timezone_identifier )

date_default_timezone_set() Sets the default time zone used for all date and time functions.

Note: Since PHP 5.1.0 (this version of date and time function has been rewritten), if the time zone is not legal, each call to the date and time function will generate an E_NOTICE level error message.

parameter

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

PS: Here are a few time and date related tools for your reference:

Online Date/Day Calculator:
http://tools./jisuanqi/date_jisuanqi

Online date calculator/phase difference day calculator:
http://tools./jisuanqi/datecalc

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

Unix timestamp conversion tool:
http://tools./code/unixtime

For more information about PHP related content, please check out the topic of this site:Summary of the usage of php date and time》、《Complete collection of PHP array (Array) operation techniques》、《Introduction to PHP basic syntax》、《Summary of PHP operations and operator usage》、《PHP object-oriented programming tutorial》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope this article will be helpful to everyone's PHP programming.