Setting time zone methods in PHP summary _php tips

Source: Internet
Author: User
Tags datetime echo date local time
After you find out why, you search the web for some time zone settings for PHP:

1, modify php.ini, found in the php.ini Data.timezone = remove it before the number, and then set data.timezone = "Asia/shanghai";

2, in the program PHP 5 version of the program code to use the function ini_set (' Date.timezone ', ' Asia/shanghai '), or Date_default_timezone_set (' Asia/shanghai ');

Some common time zone identifiers are described:

asia/shanghai– Shanghai
asia/chongqing– Chongqing
asia/urumqi– Urumqi
asia/hong_kong– Hong Kong
asia/macao– Macau
asia/taipei– Taipei
asia/singapore– Singapore


function to set the time zone method:
Copy Code code as follows:

<?php
Function_exists (Date_default_timezone_set); In this he always returns 1, this function is to judge whether this character is a defined function name
Date_default_timezone_set ("Etc/gmt");//This is Greenwich Mean time and the time is the same as the default timezone
Date_default_timezone_set ("etc/gmt+8")//This is 8 hours slower than the Linwei standard time.
Date_default_timezone_set ("etc/gmt-8"); it's 8 hours faster than the Linwei standard.
Date_default_timezone_set (' PRC '); Set China time zone
?>


function Ini_set () to set 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 to find date.timezone = "PRC" If there is remove the preceding semicolon, no words to add manually!

Here are some additional information:

you'll find this problem after you put the PHP5 on.

$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.
This is why pinch?
The reason is that if you don't set up your server's local time zone in your program or configuration file,
The time that PHP takes is Greenwich mean time, so there's a discrepancy between your local time.
Greenwich Mean Time and Beijing time about 8 hours or so how do we avoid the time error?
Let's take a look at the solution:
Use Date_default_timezone_set () in the header to set my default time zone to the
Copy Code code as follows:

?
Date_default_timezone_set (' PRC ');
echo Date (' y-m-d h:i:s ');
?>

Time is the same as the server's current time!
Additional attached date_default_timezone_set usage is as follows:
------------------------------------------------------------------------------------
Date_default_timezone_set
(PHP 5 >= 5.1.0rc1)
Date_default_timezone_set--Sets the default time zone for all date-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 date-time functions.
Note: Since PHP 5.1.0 (This version of the date time function has been rewritten), if the time zone does not rule every call to a datetime function generates a E_notice level error message.
Parameters
Timezone_identifier
Time zone identifiers, such as UTC or Europe/lisbon
return value
This function returns TRUE forever (even if the timezone_identifier parameter is illegal).
-------------------------------------------------------------------------------------
or modify the Date.timezone value in php.ini
Date.timezone = PRC

You'll find this problem after you put the PHP5 on.
$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.
This is why pinch?
The reason is that if you don't set up your server's local time zone in your program or configuration file,
The time that PHP takes is Greenwich mean time, so there's a discrepancy between your local time.
Greenwich Mean Time and Beijing time about 8 hours or so how do we avoid the time error?
Let's take a look at the solution:
Use Date_default_timezone_set () in the header to set my default time zone to the
Copy Code code as follows:

?
Date_default_timezone_set (' PRC ');
echo Date (' y-m-d h:i:s ');
?>

Time is the same as the server's current time!
Additional attached date_default_timezone_set usage is as follows:
------------------------------------------------------------------------------------
Date_default_timezone_set
(PHP 5 >= 5.1.0rc1)
Date_default_timezone_set--Sets the default time zone for all date-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 date-time functions.
Note: Since PHP 5.1.0 (This version of the date time function has been rewritten), if the time zone does not rule every call to a datetime function generates a E_notice level error message.
Parameters
Timezone_identifier
Time zone identifiers, such as UTC or Europe/lisbon
return value
This function returns TRUE forever (even if the timezone_identifier parameter is illegal).
-------------------------------------------------------------------------------------
or modify the Date.timezone value 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.