PHP date () function output dates incorrect workaround _php tutorial

Source: Internet
Author: User
A friend who used PHP for the first time may find that when we configure the PHP environment, we use the PHP date function to output the date and find that the date is related to the correct date for 8 hours.

Cases

The code is as follows Copy Code

echo Date (' y-m-d h:i:s ');
?〉

Output Current time: 2008-10-12-02:32:17
Strange, the actual time is: 2008-10-12-10:32:17

is PHP date () 8 hours less than the correct time?

Take a look at the PHP manual "Example 1." Date () example "The first line has one more time zone set
Set the default time zone to use. Available from PHP 5.1

The code is as follows Copy Code

Date_default_timezone_set (' UTC ');

Originally php5.1. Start, PHP.ini added date.timezone this option, by default, is closed, that is, the time displayed (no matter what PHP command) is Greenwich Mean Time, and Beijing time is just 8 hours.

How to set up to get the correct PHP time?

1, modify the php.ini. Open php.ini Find Date.timezone Remove the preceding semicolon = add Asia/shanghai After, restart Apache server can--the disadvantage is that if the program

Put on someone else's server, can't modify php.ini, we can modify the PHP program OH

The code is as follows Copy Code


Date_default_timezone_set (' asia/chongqing ');
echo Date (' y-m-d h:i:s ');
?>


Directly plus 8 hours of seconds

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.html www.bkjia.com true http://www.bkjia.com/PHPjc/632080.html techarticle a friend who used PHP for the first time may find that when we configure the PHP environment, we use the PHP date function to output the date and find that the date is related to the correct date for 8 hours. Example code to copy code as follows ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.