Home>Article>Backend Development> What should I do if the time displayed in php is different from the actual time?
Solution: 1. Use "ini_set('date.timezone','PRC')" to set the time zone; 2. Use "date_default_timezone_set('PRC')" to set the time zone; 3. In the configuration file, Set "date.timezone=PRC".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, the displayed time is inconsistent with the actual time , because the time zone setting is wrong.
The world is divided into 24 time zones. Each time zone has its own local time. The local time of each time zone differs from 1 to 23 hours at the same time. For example, the local time in London, UK, and the local time in Beijing differ by 8 hours. .
If you want to set the displayed time to be the same as the actual time, you need to set the time zone to the local time zone. Here are some common time zone identifiers and their meanings:
Asia /Shanghai —— Shanghai
Asia/Chongqing —— Chongqing
Asia/Urumqi —— Urumqi
Asia/Hong_Kong —— Hong Kong
Asia/Macao —— Macao
Asia/Taipei —— Taipei
Asia/Singapore - Singapore
PRC - China Time Zone
Note: If we are in China, we will change the time zone It can be set to "Asia/Shanghai
orPRC
". Remember to restart the server after the setting is successful!
Let’s take a look at how to set the time zone:
Method 1:Use the ini_set() function to set the time zone
In PHP The ini_set() function can set the value of a specified configuration option. This configuration option will maintain the new value while the script is running, and restore it when the script ends. The syntax format of the function is as follows:
ini_set($varname, $newvalue)
where $varname is the configuration option to be modified, and $newvalue is the new value of the configuration option.
Example:
'; ini_set('date.timezone', 'PRC'); echo '国内当前时间为:'.date('Y-m-d H:i:s',time()); ?>
Output result:
##Method 2: Use date_default_timezone_set() function Set time zone
The date_default_timezone_set() function in PHP can set a default time zone for all time and date functions in the script. The syntax format is as follows:date_default_timezone_set($timezone_identifier)The parameter $timezone_identifier is the time zone identifier symbol, such as UTC (Greenwich Mean Time) or Europe/Lisbon (Portugal). Since PHP5.1.0 (the date and time function has been rewritten in this version), if the time zone is illegal, each call to the date and time function will generate an E_NOTICE level error message. If you use the system setting or The TZ environment variable also generates E_STRICT level information. Example:
'; date_default_timezone_set('PRC'); echo '国内当前时间为:'.date('Y-m-d H:i:s',time()); ?>Output result: ##Method 3: Set
in the configuration file php.iniThe configuration file of PHP is the php.ini file in the PHP installation directory. Find and open it. Then search for date.timezone in the file, we will see a piece of information as shown below:
[Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone ;date.timezone =
where
;date.timezone =the;
in front of it is the same as PHP The//
has the same function and means a comment. Here we need to remove this; and fill in the corresponding time zone identifier after=
in this sentence, as shown below :[Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone date.timezone = PRC
Asia/Aden | Asia/Almaty | Asia/Amman | Asia/Anadyr |
Asia/Aqtau | Asia/Aqtobe | Asia/Ashgabat | Asia/Atyrau |
Asia/Baghdad | Asia/Bahrain | Asia/Baku | Asia/Bangkok |
Asia/Barnaul | Asia/Beirut | Asia/Bishkek | Asia/Brunei |
Asia/Chita | Asia/Choibalsan | Asia/ Colombo | Asia/Damascus |
Asia/Dhaka | Asia/Dili | Asia/Dubai | Asia /Dushanbe |
Asia/Famagusta | Asia/Gaza | Asia/Hebron | Asia/Ho_Chi_Minh |
Asia/Hong_Kong | Asia/Hovd | Asia/Irkutsk | Asia/Jakarta |
Asia/ Jayapura | Asia/Jerusalem | Asia/Kabul | Asia/Kamchatka |
Asia/Karachi | Asia /Kathmandu | Asia/Khandyga | Asia/Kolkata |
Asia/Krasnoyarsk | Asia/Kuala_Lumpur | Asia/Kuching | Asia/Kuwait |
Asia/Macau | Asia/Magadan | Asia/Makassar | Asia/Manila |
Asia/Muscat | Asia/Nicosia | Asia/Novokuznetsk | Asia/Novosibirsk |
Asia/Omsk | Asia/Oral | Asia/Phnom_Penh | Asia/Pontianak |
Asia/Pyongyang | Asia/Qatar | Asia/Qostanay | Asia/Qyzylorda |
Asia/Riyadh | Asia/Sakhalin | Asia/Samarkand | Asia/Seoul |
Asia/Shanghai | Asia/Singapore | Asia/Srednekolymsk | Asia/Taipei |
Asia/Tashkent | Asia/Tbilisi | Asia/Tehran | Asia/Thimphu |
Asia/Tokyo | Asia/Tomsk | Asia/Ulaanbaatar | Asia/Urumqi |
Asia/Ust-Nera | Asia/Vientiane | Asia/Vladivostok | Asia/Yakutsk |
Asia/Yangon | Asia/Yekaterinburg | Asia/Yerevan |
PHP视频教程》
The above is the detailed content of What should I do if the time displayed in php is different from the actual time?. For more information, please follow other related articles on the PHP Chinese website!