Workaround for 8 hours of date time in PHP

Source: Internet
Author: User
Tags time zones
workaround for 8 hours of date time in PHP

On Windows , in the default PHP configuration, the time value returned by the DATE function is always 8 hours from the local time, that is, the GMT is displayed. The solution is to set the PHP time zone environment correctly.

The time zone environment for PHP is specified by a specific set of strings. See the list of time zones supported by PHP.

The easiest way is to find the Date.timezone line in the php.ini file, which is commented out by default, and can be enabled by deleting the previous semicolon. For mainland China, it can be set to etc/gmt-8(or Asia/sanghai,asia/chongqing,asia/urumqi,PRC ).

If for some reason you cannot modify php.ini, you can use Date_default_timezone_set () settings in your PHP program. For example, you can do this before using the DATE function

Date_default_timezone_set (' etc/gmt-8 ');

I personally think it is better to use the Date_default_timezone_set function. One is the versatility, and secondly, if your site is for global users, this method allows each user to set his time zone.

Finally, it is necessary to illustrate the wording of ETC/GMT. China is using the East eight region of the time, under normal circumstances are recorded as +8, but PHP has come a big anti-height, to write etc/gmt-8; Conversely, if your time zone is the West 2 zone, in PHP will be written etc/gmt+2. This is different from common sense, especially to note.


The above is the PHP date time difference 8 hours of solution content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!


  • 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.