Solution to an eight-hour difference in the date time in php

Source: Internet
Author: User
Solution to an eight-hour difference in the date time in php Solution to an eight-hour difference in the date time in php

In Windows, in the default PHP configuration, the time value returned by the date function is always 8 hours different from the local time, that is, the Greenwich mean time is displayed. The solution is to correctly set the time zone of PHP.

The time zone of PHP is specified by a specific string. See the list of time zones supported by PHP.

The most convenient way is to find a line of date. timezone in the php. ini file. by default, this line is commented out and can be enabled by deleting the semicolon. For mainland China, you can set itEtc/GMT-8(OrAsia/Sanghai,Asia/Chongqing,Asia/Urumqi,PRC).

If you cannot modify php. ini for some reason, use date_default_timezone_set () in the PHP program. For example, you can run

date_default_timezone_set('Etc/GMT-8');

I personally think it is better to use the date_default_timezone_set function. First, it is highly Universal. Second, if your website is for global users, this method allows every user to set his/her time zone.

It should be noted that The Etc/GMT method is used. Mainland China adopts the UTC + 8 zone time, generally are recorded as + 8, but PHP comes a big inverse, to write Etc/GMT-8; on the contrary, if your time zone is in the West 2 region, you need to write Etc/GMT + 2 in PHP. This is different from common sense.


The above is the solution for php's 8-hour difference in date time. For more information, see PHP Chinese website (www.php1.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.