A summary of PHP and MySQL time zones

Source: Internet
Author: User
Tags time zones
Due to the temporary use of overseas space, I found that the time was always wrong when I published a Blog. Based on my previous programming experience, this is a matter of time zone. It is not difficult to solve this problem. write down my solutions for future reference. Due to the temporary use of overseas space, I found that the time was always wrong when I published a Blog. Based on my previous programming experience, this is a matter of time zone. It is not difficult to solve this problem. write down my solutions for future reference.

You can set the value of the date. timezone key to 'Asia/Shanghai' in the urban configuration of the PHP script in php. ini. However, the shared VM itself does not have the permission to modify php. ini. This should be added to the public part of the program.

Ini_set ('date. timezone ', 'Asia/Shanghai ');

Modify php. ini settings dynamically. Then you can test whether the time is correct:

Var_dump (date ());

If the local time of the server is correct, the problem can be solved. Appendix: PHP 5.1 and above provide special functions to modify the corresponding time zone:

Date_default_timezone_set ('Asia/Shanghai ');

We recommend that you use this function because it is more common. Corresponding to 'Asia/Shanghai', other available mainland time zones include Asia/Chongqing, Asia/Shanghai, and Asia/Urumqi (Chongqing, Shanghai, and Urumqi in sequence). Hong Kong and Taiwan regions are available: asia/Macao, Asia/Hong_Kong, Asia/Taipei (Macau, Hong Kong, and Taipei in sequence), Singapore: Asia/Singapore, and other available values are: etc/GMT-8, Singapore, Hongkong, PRC; foreigners seem to have missed Beijing.

However, after I successfully modified the time zone on the PHP side, I found that the date was not properly recorded. At this time, I will consider whether it is a database issue. Sure enough, because the function inserted by the program does not call the PHP time, instead, it directly uses the MySQL CURRECT_TIMESTAMP. In this case, you must consider whether you can modify the MySQL time zone.

Referring to the MySQL documentation, we found a feasible SQL statement:

Set global time_zone = '+ 8:00 ';

'+' Indicates the Dongba district, and other urban areas are like this. I inserted a modified statement in the database model and found that the permission was insufficient (the damn VM provider ). Next, I debugged many statements, such:

DATE_ADD (UTC_TIMESTAMP (), INTERVAL 8 HOUR );

SQL statement for displaying the time zone:

Show variables like 'system _ time_zone'

And so on. However, the restrictions on MySQL permissions do not provide a thorough solution. I Googled and found that foreigners have a very good solution. But he needs to modify the SQL statement of each inserted data. This solution is not very effective. Once the database time zone is changed to normal, the corresponding SQL statement will be changed back.

I think that since the PHP end can solve the problem of time correctly. Although the corresponding functions can be used for MySQL databases, it will be changed back if it is migrated to another host environment in the future. The corresponding field is of the TIMESTAMP type, and the default value is CURRECT_TIMESTAMP. of course, the time can be specified.

So my approach is to let PHP insert the current correct time, although the program needs to be modified accordingly. However, you only need to modify the configuration once later. Note the format when inserting the database:

Date ('Y-m-d H: I: s') to solve the problem. Appendix, some very good references:

This will solve the problem. Appendix, some very good references:

Http://www.modwest.com/help/kb6-256.html

Http://topic.csdn.net/t/20060503/07/4728521.html

Http://www.phpchina.com/5173/viewspace_5132.html

Http://www.phpx.com/pth110355.php

Update: wiLdGoose said that he encountered the same problem but could not solve it. After various assumptions and judgments, I finally found the time zone configuration problem of Zend Studio (I am sweating ). In addition to the running environment, the development environment also needs to pay attention to the following.

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.