Workaround for time zone problems encountered in PHP

Source: Internet
Author: User
Tags echo date what php

Recently in the process of learning PHP found PHP format timestamp 8 hours later than Beijing time, internet search found that the original time zone is not correct, the solution is:

1. Permanent modification

Change Data.timezone in php.ini file = PRC This is China time. The Apache service is restarted after the operation.

2. Temporary modification

Add Ini_set (' Date.timezone ', ' Asia/shanghai ') before using the formatted input time;
or Data_default_timezone_set (' PRC '); the//date_default_timezone_set () function sets the default time zone for all date/time functions used in the script.

Look at the problems that other small partners have encountered

One legacy of the previous problem is that echo date ("Y-m-d h:i:s", Time ()) is always back with the actual time, and today finally found the reason and solution on the Internet, share the following:

Add a little, according to the method suggested below to modify php.ini can not find Date.timezone this line, is not there is no way, of course, no, haha no words on their own add, own hands and clothing. Add such a sentence Date.timezone = "PRC", the problem is done, happy

Starting with php5.1.0, PHP.ini added the Date.timezone option, which is turned off by default

That is, the time displayed (no matter what PHP command) is Greenwich Mean time, and our time (Beijing time) is about 8 hours, the following 3 methods can restore normal time.

1, the simplest way is not to use the version of php5.1 above

2, if must be used, and can not modify the php.ini, you need to be on the initialization of the time of the statement on the top plus date_default_timezone_set (XXX);

3, once and for all, can only modify php.ini. Open php.ini Find Date.timezone remove the preceding semicolon
= Add xxx After, restart HTTP service (such as Apache2 or IIS, etc.)

For XXX, the values available in mainland China are: asia/chongqing, Asia/shanghai, Asia/urumqi (Chongqing, Shanghai, Urumqi, respectively)

Taiwan available: Asia/macao, Asia/hong_kong, Asia/taipei (Macau, Hong Kong, Taipei, respectively)

and Singapore: Asia/singapore

The foreigner seems to have missed Beijing's tune.

Other available values are: Etc/gmt-8, Singapore, Hongkong, PRC

What is PRC? PRC is the People's Republic of China Ah-_-

———————————————————————————————————————

Workaround: Use Date_default_timezone_set () in the header to set my default time zone to Beijing time Date_default_timezone_set (' PRC ');
echo Date (' y-m-d h:i:s '); Time is the same as the current time of the server!! Congratulations on the attached Date_default_timezone_set usage as follows

——————————–

Date_default_timezone_set
(PHP 5 >= 5.1.0rc1)
date_default_timezone_set-Setting the default time zone description for all datetime functions in a script
BOOL Date_default_timezone_set (String timezone_identifier)
Date_default_timezone_set () Sets the default time zone for all datetime functions.
Note: Since PHP 5.1.0 (This version of the date time function is rewritten), each call to a DateTime function will produce an E_notice level error message if the time zone is not in law.

Above mentioned above is the whole content of this article, hope everybody can like.

Workaround, PHP
  • 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.