Home >Backend Development >PHP Tutorial >Solutions to time zone problems encountered in PHP, php time zone_PHP tutorial

Solutions to time zone problems encountered in PHP, php time zone_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:45:571000browse

Solution to time zone problems encountered in PHP, php time zone

Recently, while learning PHP, I discovered 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 and shared it. As follows:

One more thing, if you can’t find the date.timezone line by modifying php.ini according to the method below, there is nothing you can do about it. 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, I'm 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 above

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, 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 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. The usage of date_default_timezone_set is 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1037591.htmlTechArticleSolution to time zone problems encountered in PHP, php time zone Recently, I discovered the formatting in PHP during the process of learning PHP The timestamp is 8 hours later than Beijing time. Searching online found that the time zone is different...
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