How to solve the date time difference problem in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:37:25
Original
979 people have browsed it

The time when date("Y-m-d H:i:s") is encountered today is always inconsistent with the actual time. So I checked the relevant information and found out the reasons, which are summarized as follows.

The reason for this phenomenon:
Starting from php5.1.0, the date.timezone option has been added to php.ini. It is turned off by default, which is the displayed time zone. By default, the difference between Greenwich Mean Time and our time (Beijing time) is exactly 8 hours.
Solution:
1. The simplest way is not to use php5.1 or above
2. Modify the php.ini configuration file
You can find date.timezone in php.ini and modify it.
Under Windows, php.ini is usually located at: C:/WINDOWS/php.ini
php.ini under Linux is usually at: /etc/php.ini
Follow the above tips to find the date.timezone line under php.ini. If you can’t find it, you can add it yourself. Remove the semicolon in front and change it to date.timezone = "Asia/Shanghai". Remember to restart the http service (such as apache2 or iis, etc.).
date.timezone value reference after the semicolon,
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 (Macau, Hong Kong, Taipei in order)
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
3. If you must use php5.1 or above, and you cannot modify the php.ini configuration file.
(1) Add 8 hours when processing time in PHP code to get the correct Chinese time date(‘Y-m-d H:i:s’, time()+8*3600);
(2) Add date_default_timezone_set (XXX) to the initialization statement about time, or use date_default_timezone_set (XXX) on the header of the page to set my default time zone to Beijing time date_default_timezone_set ('Asia/Shanghai') can also be used.
The format string can recognize the string of the following format parameter
format character description return value example
d Day of the month, 2 digits with leading zero 01 to 31
D Day of the week, text representation, 3 letters Mon to Sun
j Day of the month, without leading zero 1 to 31
l (lower case letter of "L") Day of the week, complete text format Sunday to Saturday
N The day of the week represented by numbers in ISO-8601 format (newly added in PHP 5.1.0) 1 (meaning Monday) to 7 (meaning Sunday)
S The English suffix after the day of the month, 2 characters st, nd, rd or th. Can be used with j
w The day of the week, represented by numbers 0 (meaning Sunday) to 6 (meaning Saturday)
z Day of the year 0 to 365
Week
W The week number in the year in ISO-8601 format, each week starts on Monday (newly added in PHP 4.1.0) For example: 42 (the 42nd of the year week)
Month
F Month, complete text format, such as January or March January to December
m Numeric month, with leading zeros 01 to 12
M Three letter abbreviation for the month Jan to Dec
n Numeric month, without leading zeros 1 to 12
t Number of days in a given month 28 to 31
Year
L Whether it is a leap year If it is a leap year, it is 1, otherwise it is 0
o Year number in ISO-8601 format. This is the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used. (New in PHP 5.1.0) Examples: 1999 or 2003
Y Full 4-digit year For example: 1999 or 2003
y 2-digit year For example: 99 or 03
Time
a Lowercase AM and PM values am or pm
A Capital AM and PM values AM or PM
B Swatch Internet standard time 000 to 999
g Hour, 12-hour format, no leading zeros 1 to 12
G Hour, 24-hour format, no leading zeros 0 to 23
h Hour, 12-hour format, with leading zeros 01 to 12
H Hour, 24-hour format, with leading zeros 00 to 23
i Minutes with leading zeros 00 to 59>
s Seconds, with leading zeros 00 to 59>
Time Zone
e Time zone identifier (new in PHP 5.1.0) For example: UTC, GMT, Atlantic/Azores
I Whether it is daylight saving time 1 if it is daylight saving time, otherwise 0
O The number of hours from Greenwich Mean Time Example: +0200
P The difference from Greenwich Mean Time (GMT), there is a colon separating hours and minutes (newly added in PHP 5.1.3) For example: +02:00
T The time zone where this machine is located For example: EST, MDT ([Translator's Note] In complete text format under Windows, such as "Eastern Standard Time", the Chinese version will Display "China Standard Time").
Z Time difference offset in seconds. Time zone offsets west of UTC are always negative, and time zone offsets east of UTC are always positive. -43200 to 43200
Full date/time
c Date in ISO 8601 format (new in PHP 5) 2004-02-12T15:19:21+00:00
r Date in RFC 822 format Example: Thu, 21 Dec 2000 16:01:07 +0200
U The number of seconds since the Unix epoch (January 1 1970 00:00:00 GMT) See time()
Category: PHP

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735872.htmlTechArticleThe time when I encounter date(Y-m-d H:i:s) today is always inconsistent with the actual time. So I checked the relevant information and found out the reasons, which are summarized as follows. The reason for this phenomenon: Starting from php5.1.0,...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!