Summary of setting the time zone in PHP

Source: Internet
Author: User
Today, I found a problem with the time judgment statement in a piece of PHP code. After studying it, I found that the problem lies in the time zone settings in PHP. The default time taken by PHP is Greenwich Mean Time, so it is 8 hours different from Beijing time.

Today, I found a problem with the time judgment statement in a piece of PHP code. After studying it, I found that the problem lies in the time zone settings in PHP. The default time taken by PHP is Greenwich Mean Time, so it is 8 hours different from Beijing time.

After finding the cause, I found some methods for setting the time zone of PHP on the Internet:

1. Modify php. ini, find data. timezone = in php. ini, remove the; sign, and set data. timezone = "Asia/Shanghai.

2. Use the ini_set function ('date. timezone ', 'Asia/Shanghai') in the program code of PHP version 5 or later; or date_default_timezone_set ('Asia/Shanghai ');

Common time zone identifiers:

Asia/Shanghai-Shanghai
Asia/Chongqing-Chongqing
Asia/Urumqi-Urumqi
Asia/Hong_Kong-Hong Kong
Asia/Macao-Macao
Asia/Taipei-Taipei
Asia/Singapore-Singapore


How to set the time zone of a function:
The Code is as follows:
Function_exists (date_default_timezone_set); // at this point, he always returns 1. This function is used to determine whether the character here is a defined function name.
Date_default_timezone_set ("Etc/GMT"); // This is the Greenwich Mean Time. The obtained time is the same as the default time zone.
Date_default_timezone_set ("Etc/GMT + 8"); // This is 8 hours slower than the standard time of linwei governance
Date_default_timezone_set ("Etc/GMT-8"); // This is 8 hours faster than linwei's Standard Time
Date_default_timezone_set ('prc'); // set the China Time Zone
?>


The ini_set () function sets the time zone:
You can add ini_set ('date. timezone ', 'Asia/Shanghai') at the beginning of the file; // 'Asia/Shanghai' is the Shanghai time zone.

Manually modify php. ini settings
Open php and find date. timezone = "PRC". If the semicolon is removed, add it manually!

Here are some additional information:

You will find this problem after installing PHP5

$ Atime = date ("Y-m-d H: I: s ");
Echo $ atime;
?>
Output: 06:36:06
What time is it now? /Mine is
Why is this pinch?
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 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 page 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 server time!
The usage of date_default_timezone_set is as follows:
Bytes ------------------------------------------------------------------------------------
Date_default_timezone_set
(PHP 5> = 5.1.0RC1)
Date_default_timezone_set -- set the default time zone used 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 for all datetime functions.
Note: Since PHP 5.1.0 (this version of the datetime function has been rewritten), if the time zone does not comply with the rules, every call to the datetime 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 invalid ).
Bytes -------------------------------------------------------------------------------------
Or modify the value of date. timezone in php. ini.
Date. timezone = PRC

You will find this problem after installing PHP5
$ Atime = date ("Y-m-d H: I: s ");
Echo $ atime;
?>
Output: 06:36:06
What time is it now? /Mine is
Why is this pinch?
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 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 page 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 server time!
The usage of date_default_timezone_set is as follows:
Bytes ------------------------------------------------------------------------------------
Date_default_timezone_set
(PHP 5> = 5.1.0RC1)
Date_default_timezone_set -- set the default time zone used 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 for all datetime functions.
Note: Since PHP 5.1.0 (this version of the datetime function has been rewritten), if the time zone does not comply with the rules, every call to the datetime 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 invalid ).
Bytes -------------------------------------------------------------------------------------
Or modify the value of date. timezone in php. ini.
Date. timezone = PRC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.