How to set the time zone in php. The default time zone of php is in Europe and America, so it is 8 hours different from our China time zone. next I will introduce you to the php time zone setting method. if you need to know about it, please refer to it. The default time zone of php is in Europe and America, so it is 8 hours different from our China time zone. next I will introduce you to the php time zone setting method. if you need to know about it, please refer to it.
In php. ini, the default value is date. timezone = UTC. Change to China time zone and date. timezone = PRC. If the GMT format is directly written, it is date. timezone = Etc/GMT + 8.
You can also set it in the PHP page header.
The code is as follows: |
|
Date_default_timezone_set ('prc '); |
In PHP5, there are many ways to set or obtain the default time zone settings. for example, you can use the date_default_timezone_setl function to set the time zone.
The code is as follows: |
|
Date_default_timezone_set ("Asia/Shanghain"); // Set the time zone to Shanghai. ?> Or set the Tokyo time zone code: Date_default_timezone_set ("Asia/Tokyo "); ?> When the system is initialized, add Ini_set ('date. timezone ', 'Asia/Shanghai '); Or Date_default_timezone_set ("PRC "); |
The time zone difference is 8.
If you have php. ini management permissions, you can directly modify them in php. ini.
Manually modify php. ini settings
Open php and find date. timezone = "PRC". if the semicolon is removed, add it manually!
After installing PHP5, you will find the following problems:
The code is as follows: |
|
$ Atime = date ("Y-m-d H: I: s "); Echo $ atime; ?> $ Atime = date ("Y-m-d H: I: s "); Echo $ atime; ?>
|
You may find that the output time is different from the current time.
The reason is that if you do not set the local time zone of your server in the program or configuration file, the time taken by PHP is the Greenwich mean time, so it may be different from your local time.
The GMT standard time is about eight hours behind Beijing time. how can we avoid time errors?
Let's take a look at the solution:
Use date_default_timezone_set () in the header to set my default time zone to Beijing time.
The code is as follows: |
|
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.
If a database insertion error occurs, make sure that H in date ('Y-m-d H: I: s') is in uppercase.
....