Some summary of PHP and MySQL time zone settings

Source: Internet
Author: User
Tags time zones php and mysql
Friends who use foreign host space often encounter problems with time zone settings. The PHP script side of the urban settings can be set under PHP.ini date.timezone key value of ' Asia/shanghai ' can be.

Friends who use foreign host space often encounter problems with time zone settings. The PHP script side of the urban settings can be set under PHP.ini date.timezone key value of ' Asia/shanghai ' can be. Typically, the shared virtual host itself does not modify php.ini permissions. At this point should be added in the public part of the program: Ini_set (' Date.timezone ', ' Asia/shanghai '); Dynamically modifies the settings of the php.ini. You can then test whether the time is correct:

Var_dump (date ()); If the local time of the server is correct, then the problem will be solved in general. Attached, PHP 5.1 above provides a special function to modify the corresponding time zone: Date_default_timezone_set (' Asia/shanghai '); This function is recommended because it is more generic. Other continental time zones that correspond to ' Asia/shanghai ' can be used: asia/chongqing, Asia/shanghai, Asia/urumqi (Chongqing, Shanghai, Urumqi, respectively); Taiwan available: Asia/macao, asia/ Hong_Kong, Asia/taipei (Macao, Hong Kong, Taipei) and Singapore: Asia/singapore; Other available values are: Etc/gmt-8, Singapore, Hongkong, PRC; the foreigner seems to have leaked Beijing.

However, after I modified the time zone of the successful PHP end, the date was not recorded correctly. This time I consider whether it is a database problem. Sure enough, because the program inserted function does not call PHP time, but the direct use of MySQL currect_timestamp. This time, consider whether you can modify the MySQL time zone.

Referring to the MySQL documentation, a workable SQL statement was found: SET GLOBAL time_zone = '; Where ' "" is the East eight district of the expression method, the other urban areas in turn. And I insert a change statement in the database model to discover that permissions are insufficient. Then debug a lot of statements, such as: Date_add (Utc_timestamp (), INTERVAL 8 HOUR); The SQL statement that displays the time zone: show VARIABLES like ' System_time_zone ' and so on. There is no complete solution due to the limitations of MySQL permissions. I Google, found that foreigners this has a very good solution. But he needs to modify each SQL statement that inserts data. Such a scheme is not very effective, once the database time zone is changed to normal, then the corresponding SQL statement should be changed back.

And I think since the PHP side has been able to correctly solve the problem of time. MySQL database aspects can be resolved with the corresponding function, but if later migrated to another host environment and change back. and the corresponding field is a TIMESTAMP type, the default value is Currect_timestamp, of course, you can specify the time.

My approach is to have PHP insert the current correct time, so that although the program needs to make the appropriate changes. However, the configuration changes in the future as long as you modify one place. Last time you insert a database note the format: Date (' y-m-d h:i:s ')

Reference article: Http://www.modwest.com/help/kb6-256.html on its own host can be set up: Set GLOBAL time_zone = ' "; Unfortunately, it is not possible to use a function like Unix_timestamp ().

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