Home > Article > Backend Development > Solutions to time zone issues encountered in PHP
Recently, while learning PHP, I found that the formatted timestamp in PHP is 8 hours later than Beijing time. After searching online, I found that the time zone is wrong. The solution is: 1. Permanent modification Change data.timezone = PRC in the php.ini file. This is China time. It will take effect after restarting the Apache service. 2. Temporary modification Before using formatted input time, add ini_set('date.timezone','Asia/Shanghai'); Look at the problems encountered by other friends There was a legacy problem before, that is, the time returned by echo date("Y-m-d H:i:s", time()) always did not match the actual time. Today, I finally found the reason and solution online. I share it as follows: One more thing, follow the tips below to modify php.ini. If you can’t find the date.timezone line, is there nothing you can do? Of course not, haha, if not, just add it yourself, and you will have enough food and clothing by yourself. Adding date.timezone = "PRC", the problem is solved, happy Starting from php5.1.0, the date.timezone option has been added to php.ini, which is turned off by default That is, the displayed time (no matter what php command is used) is Greenwich Mean Time, which is exactly 8 hours different from our time (Beijing time). There are three methods below to restore the normal time. 1. The simplest way is not to use php5.1 or higher version 2. If you must use it and cannot modify php.ini, you need to add date_default_timezone_set (XXX); 3. Once and for all, only php.ini can be modified. Open php.ini and search for date.timezone and remove the semicolon in front Regarding XXX, the available values in mainland China are: Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (in order, Chongqing, Shanghai, and Urumqi) Available in Hong Kong and Taiwan: Asia/Macao, Asia/Hong_Kong, Asia/Taipei (Macau, Hong Kong, Taipei in order) And Singapore: Asia/Singapore Foreigners seem to have missed Beijing Other available values are: Etc/GMT-8, Singapore, Hongkong, PRC What is PRC? PRC is the People's Republic of China -_- —————————————————————————————————————— Solution: Use date_default_timezone_set() in the header of the page to set my default time zone to Beijing time date_default_timezone_set('PRC'); ——————————– date_default_timezone_set The above is the entire content of this article, I hope you all like it. |