Home  >  Article  >  Backend Development  >  Solutions to time zone issues encountered in PHP

Solutions to time zone issues encountered in PHP

WBOY
WBOYOriginal
2016-07-25 08:44:36834browse

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');
Or data_default_timezone_set('PRC');//date_default_timezone_set() function sets the default time zone used for all date/time functions in the script.

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);
above the initialization statement about time.

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
= Add XXX after it and restart the http service (such as apache2 or iis, etc.)

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');
echo date('Y-m-d H:i:s'); The time is the same as the current time of the server!! Congratulations. Attached is the usage of date_default_timezone_set as follows

——————————–

date_default_timezone_set
(PHP 5 >= 5.1.0RC1)
date_default_timezone_set — Set the default timezone specification used for all datetime functions in a script
bool date_default_timezone_set ( string timezone_identifier )
date_default_timezone_set() sets the default time zone used for all datetime functions.
Note: Since PHP 5.1.0 (the date and time functions have been rewritten in this version), each call to the date and time function will generate an E_NOTICE level error message if the time zone is illegal.

The above is the entire content of this article, I hope you all like it.

Solution, PHP


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn