Home>Article>PHP Framework> How to set time zone in thinkphp3.2
How to set the time zone: 1. Set the time zone in the "php.ini" configuration file, change the content of the "date.timezone" item and remove the ";" in front of the item; 2. Use " "date_default_timezone_set()" function sets the time zone, just specify the specified time zone in the parameters.
The operating environment of this article: Windows 10 system, ThinkPHP version 3.2, Dell G3 computer.
If your servers are distributed in different regions or countries, then there may be a large distance between the server where some applications are located and the area accessed, resulting in server time inconsistency. precise.
For the same timestamp, if the time zone is different, the obtained date and time string will be different. Therefore, setting the correct time zone is essential.
Different time zones will affect the value of date('Y-m-d H:i:s', time()) or similar functions.
We can handle this by setting the default time zone.
The location to modify the time zone is roughly as follows:
1. Set the time zone in the PHP.INI configuration file:
File location/usr/local/ php/etc/php.ini
date.timezone = PRC
2. Set the time zone in the code:
2-1. File location :/api/public/init.php
date_default_timezone_set('Asia/Phnom_Penh')//Required: Cambodia time zone
Commonly used time zones are as follows:
a).date_default_timezone_set('Asia/Shanghai');//'Asia/Shanghai' Asia/Shanghai
b) date_default_timezone_set('Asia/Chongqing');//where Asia/Chongqing' is "Asia/Chongqing"
c) date_default_timezone_set('PRC');//where PRC is "People's Republic of China"
d) ini_set('date.timezone','Etc/GMT-8');
e) ini_set('date.timezone','PRC');
f) ini_set('date.timezone','Asia/Shanghai');
d) ini_set('date.timezone', 'Asia/Chongqing');
After many tests and use: it is recommended to use date_default_timezone_set to set the time zone here, the effect is obvious.
2-2. File location:/application/Common/Conf/config.php
'DEFAULT_TIMEZONE'=>'Asia/Phnom_Penh',//Time zone setting: Cambodia time zone
3. Modify the server computer time setting
Function annotations in the above code:
Definition and usage
1.date_default_timezone_set(timezone) Function sets the default time zone used by all date/time functions in the script.
Parameters: timezone: required. Specifies the time zone to use, such as "UTC" or "Europe/Paris".
List of legal time zones: http://www.php.net/manual/zh/timezones.php
2. ini_set is used to set the value of php.ini, when the function is executed It takes effect when the script ends, and the setting becomes invalid after the script ends. You can modify the configuration without opening the php.ini file, which is very convenient for virtual spaces.
Function format:
string ini_set(string $varname, string $newvalue)
At this point, all settings for modifying the time zone are completed, realizing PHP time maintenance consistent.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to set time zone in thinkphp3.2. For more information, please follow other related articles on the PHP Chinese website!