Solution to why the time obtained by date() in php5 is not the current time

PHPz
Release: 2019-02-12 11:24:21
Original
1101 people have browsed it

Starting from php5.10, time zone settings have been added to php. The time displayed in php is Greenwich Mean Time. This has caused a problem of eight hours difference for our Chinese users! [Recommended tutorial: php introductory tutorial]

The relevant setting is to modify the date.timezone parameter in php.ini:

[Date]
; Defines the default timezone used by the date functions
;date.timezone =
Copy after login

The default is closed, just remove the comment and change it to

[Date]
; Defines the default timezone used by the date functions
date.timezone = PRC
Copy after login

where PRC is "The People's Republic of China"!

For other options, please refer to the php manual.

But the Asian region above misses our capital Beijing. I don’t know if the foreigner did it on purpose!

If you do not have permission to modify php.ini, you only need to call date_default_timezone_set ('

PRC') when calling the time and date function!

You can also call date_default_timezone_get() to view the current time zone setting!

About XXX, the available values ​​in mainland China are:

Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (in order Chongqing, Shanghai, Urumqi)

Available values ​​in Hong Kong and Taiwan: Asia/Macao, Asia/Hong_Kong, Asia /Taipei (Macau, Hong Kong, Taipei in order)

Taiwan area can be set to: date.timezone = "Asia//Taipei"

And Singapore: Asia/Singapore

Solution to the eight-hour time difference in PHP5

After installing php5, I accidentally saw someone on the forum saying that the time display of php5.1.2 was 8 hours shorter.

<?php
echo date("Y-m-d H:i:s");
?>
Copy after login

The result was that the difference was 8 hours after my own test.

Later, after looking for information on the forum, the solution was finally solved. In php5 and above versions, to output the local time (only in China)

, you can write the code like this:

<?php
date_default_timezone_set(&#39;Asia/Shanghai&#39;);
echo date(&#39;Y-m-d H:i:s&#39;);
?>
Copy after login

You can also write the code like this:

<?php
date_default_timezone_set(&#39;Asia/Chongqing&#39;);
echo date(&#39;Y-m-d H:i:s&#39;);
?>
Copy after login

This solves the problem of eight hours difference in time! ! ~~~


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