PHP: Using the date function to get the current time error _php tutorial

Source: Internet
Author: User
Tags add time echo date time zone codes what php
Beginners PHP To do the site, want to get the current time on the page, learn programming people know the time function date (), first Use this function to format a local time/date, first write a test code bar, the result output time is 8 hours less than the actual time, what is the reason for this:


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

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
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 can 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 not modify the php.ini, that will not be.

2, in the program to add time to initialize the statement that is: "Date_default_timezone_set (" Asia/shanghai "); "This can be arbitrarily set by the programmer, my recommendation."
Time zone identifiers, the values available in mainland China are: prc,asia/chongqing, Asia/shanghai, Asia/urumqi (Chinese, Chongqing, Shanghai, Urumqi, respectively), Etc/gmt-8,asia/harbin

Taiwan available: Asia/macao, Asia/hong_kong, Asia/taipei (Macau, Hong Kong, Taipei) and Singapore: Asia/singapore
It is time for Beijing to achieve the output.
More detailed time zone code time zone codes Time zone index code timezone_identifier you can find it on the official website.


http://www.bkjia.com/PHPjc/477500.html www.bkjia.com true http://www.bkjia.com/PHPjc/477500.html techarticle Beginners PHP To do the site, want to get the current time on the page, learn programming people know the time function date (), first Use this function to format a local time/date, first write a test generation ...

  • Related Article

    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.