The foreign space VPS PHP time is not correct solution

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags based blog change change back date environment function host

Due to the temporary use of foreign space, when I publish a blog, I found that time is always wrong. Based on my previous experience writing programs, this is a time zone problem. This problem is not difficult to solve, write down my solution for future reference.

PHP script-side urban settings can be set in php.ini date.timezone key value is 'Asia / Shanghai' can be. But usually the shared virtual host itself does not modify php.ini permissions. This time should add ini_set ('date.timezone', 'Asia / Shanghai') in the public part of the program; dynamically modify the settings of php.ini. After that you can test the time is correct: var_dump (date ()); If the server's local time is correct, then generally can solve the problem. Attached, PHP 5.1 provides a special function to modify the corresponding time zone: date_default_timezone_set ('Asia / Shanghai'); This function is recommended, because more general. Asia / Macao, Asia / Shanghai, Asia / Urumqi (followed by Chongqing, Shanghai, Urumqi); Available in Hong Kong and Taiwan: Asia / Macao, Asia / Hong_Kong, Asia / Taipei (followed by Macau, Hong Kong and Taipei); and Singapore: Asia / Singapore; other available values ​​are: Etc / GMT-8, Singapore, Hongkong, PRC; foreigners seem to have missed Beijing. However, after I changed the time zone of the successful PHP side, I found that the date was not recorded correctly. This time I consider whether it is a database problem. Sure enough, because the function inserted by the program did not call PHP time, but directly use MySQL's CURRECT_TIMESTAMP. This time we must consider whether to amend the MySQL time zone. Reference to the MySQL documentation, found a viable SQL statement: SET GLOBAL time_zone = '+8: 00'; '+8: 00' is the representation of the eastern eight districts, followed by other urban areas and so on. And I insert a statement in the database model found insufficient permissions (damn web hosting provider). Next I debug a lot of statements, such as: DATE_ADD (UTC_TIMESTAMP (), INTERVAL 8 HOUR); SQL statement showing the time zone: SHOW VARIABLES LIKE 'system_time_zone' and so on. Due to the limitations of MySQL permissions and there is no thorough solution. I Google down, I found this foreigner has a very good solution. But he needs to modify each SQL statement that inserts the data. Such a program is not very effective, once the database time zone changed to normal, then the corresponding SQL statements have to change back. And I think since the PHP side has been the right time to solve the problem. Although the MySQL database can use the appropriate function to solve, but if you migrate to another host environment in the future have to change back. The corresponding field is a TIMESTAMP type, the default value is CURRECT_TIMESTAMP, of course, can specify the time. Then my approach is to let PHP insert the current correct time, so although the program needs to be modified accordingly. However, as long as the configuration changes in the future as long as a change on it. The last time you insert the database Note the format: date ('Ymd H: i: s') This will solve the problem. Attached, some very good reference: This will solve the problem. Update: So wiLdGoose brother said he also encountered the same problem, but can not be solved. Results After a variety of assumptions and judgments later, the end was found to be Zend Studio's time zone configuration problem (I Kuanghan ing). It seems to remove the operating environment, the development environment is also need to pay attention to the following.
Related Article

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