Summary of methods to set time zone in PHP

WBOY
Release: 2016-07-29 09:03:37
Original
822 people have browsed it
Today I found something wrong with the time judgment statement in a piece of PHP code. After some research, I found that the problem lies in the time zone setting of PHP. The time taken by PHP defaults to Greenwich Mean Time, so it is 8 hours different from Beijing time
After finding the reason, I searched on the Internet and found some time zone setting methods for PHP:
1. Modify php.ini, find data.timezone = in php.ini and remove the ; sign in front of it, and then set data.timezone = "Asia/ Shanghai"; that's it.
2. Use the function ini_set('date.timezone','Asia/Shanghai'); or date_default_timezone_set('Asia/Shanghai'); in the program code of PHP version 5 or above;
Instructions for some commonly used time zone identifiers:
Asia/Shanghai – Shanghai
Asia/Chongqing – Chongqing
Asia/Urumqi – Urumqi
Asia/Hong_Kong – Hong Kong
Asia/Macao – Macau
Asia/Taipei – Taipei
Asia/Singapore – Singapore
Function setting time zone method:
Copy code The code is as follows:
function_exists(date_default_timezone_set);//Here he always returns 1. This function is to determine whether the character inside is a defined function name
date_default_timezone_set("Etc/GMT"); //This is Greenwich Mean Time, and the time obtained is the same as the default time zone
date_default_timezone_set("Etc/GMT+8"); //This is 8 hours slower than Greenwich Mean Time
date_default_timezone_set("Etc/GMT -8");//This is 8 hours ahead of Ringwich Standard Time
date_default_timezone_set('PRC'); //Set the Chinese time zone
?>
Function ini_set() to set the time zone:
You can add ini_set( at the beginning of the file 'date.timezone','Asia/Shanghai'); // 'Asia/Shanghai' is the Shanghai time zone
Manually modify the php.ini settings
Open php and find date.timezone = "PRC" If so, remove the semicolon in front, no If so, add it manually!
The following is some additional information:
After installing PHP5 you will find this problem
$atime=date("Y-m-d H:i:s");
echo $atime;
?>
Output: 2006-05- 16 06:36:06
What time is it now?/Mine is 14:36 ​​
Why is this?
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 Greenwich Mean Time, so it will be different from your local time
Greenwich Mean Time and Beijing time are approximately The difference is about 8 hours. So how do we avoid time errors?
Let’s take a look at the solution:
Use date_default_timezone_set() on the header of the page to set my default time zone to Beijing time
Copy the code 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!
In addition, 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 time zone for all date and time functions in a script
Description
bool date_default_timezone_set ( string timezone_identifier )
date_default_timezone_set() Sets the default time zone used for all date and time functions.
Note: Since PHP 5.1.0 (the date and time functions have 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.
Parameters
timezone_identifier
Time zone identifier, such as UTC or Europe/Lisbon
Return value
This function always returns TRUE (even if the timezone_identifier parameter is illegal).
------------------------------------------------- ------------------------------------
Or modify the date.timezone value in php.ini
date. timezone = PRC
After installing PHP5 you will find this problem
$atime=date("Y-m-d H:i:s");
echo $atime;
?>
Output: 2006-05-16 06:36 :06
What time is it now?/Mine is 14:36 ​​
Why is this?
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 Greenwich Mean Time, so it will be different from your local time
Greenwich Mean Time and Beijing time are approximately The difference is about 8 hours. So how do we avoid time errors?
Let’s take a look at the solution:
Use date_default_timezone_set() on the header of the page to set my default time zone to Beijing time
Copy the code 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!
In addition, 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 time zone for all date and time functions in a script
Description
bool date_default_timezone_set ( string timezone_identifier )
date_default_timezone_set() Sets the default time zone used for all date and time functions.
Note: Since PHP 5.1.0 (the date and time functions have 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.
Parameters
timezone_identifier
Time zone identifier, such as UTC or Europe/Lisbon
Return value
This function always returns TRUE (even if the timezone_identifier parameter is illegal).
------------------------------------------------- ------------------------------------
Or modify the date.timezone value in php.ini
date. timezone = PRC

The above has introduced a summary of the methods of setting time zones in PHP, including relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!