Home > Backend Development > PHP Tutorial > Solution to incorrect date output by php date() function_PHP Tutorial

Solution to incorrect date output by php date() function_PHP Tutorial

WBOY
Release: 2016-07-13 10:57:45
Original
1595 people have browsed it

Friends who are using PHP for the first time may find that after we configure the PHP environment and use the PHP date function to output the date, we will find that the date is related to the correct date by 8 hours.

Example

The code is as follows
 代码如下 复制代码

echo date('Y-m-d H:i:s');
?〉

Copy code


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

?〉


代码如下 复制代码

date_default_timezone_set('UTC');

Output current time: 2008-10-12 02:32:17

Weird, the actual time is: 2008-10-12 10:32:17

Could it be that PHP’s date() time is incorrect and is 8 hours short?

Look at the "Example 1. date() example" in the PHP manual. There is an extra time zone setting in the first line

//Set the default time zone to use. Available since PHP 5.1

The code is as follows
代码如下 复制代码


date_default_timezone_set('Asia/Chongqing');
echo date('Y-m-d H:i:s');
?>

Copy code


date_default_timezone_set('UTC');
 代码如下 复制代码

echo date("Y-m-d H:i:s",time()+8*60*60);
?>

It turns out that since php5.1., the date.timezone option has been added to php.ini. It is turned off by default, that is, the time displayed (no matter what php command is used) is Greenwich Mean Time, and Beijing time is exactly 8 hours different.

How to set the correct PHP time?1. Modify php.ini. Open php.ini and search for date.timezone. Remove the semicolon = in front and add Asia/Shanghai at the end. Just restart the apache server - the disadvantage is that if the program If you put it on someone else’s server, you cannot modify php.ini. We can modify the php program
The code is as follows Copy code
date_default_timezone_set('Asia/Chongqing'); echo date('Y-m-d H:i:s'); ?>
Directly add the seconds of 8 hours
The code is as follows Copy code
echo date("Y-m-d H:i:s",time()+8*60*60); <🎜> ?> http://www.bkjia.com/PHPjc/632080.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632080.htmlTechArticleFriends who are using PHP for the first time may find that after we configure the PHP environment and use the PHP date function to output the date, we will find that the date and The correct date is related to 8 hours. Example code is as follows Copy the code...
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