PHP Setting Time Zone method Introduction _php Tutorial

Source: Internet
Author: User
Tags echo date
PHP default time zone is Europe and the United States, so with our Chinese time zone a full 8 hours, oh, let me introduce you to the PHP set time zone method, there is a need to know the friends can enter the reference.

In php.ini, the default is Date.timezone = UTC. Change to Chinese time zone, modified to Date.timezone = PRC. If you write the GMT format directly, it is Date.timezone = etc/gmt+8.

Alternatively, you can set it in the PHP page header.

The code is as follows Copy Code

Date_default_timezone_set (' PRC ');


In PHP5, there are many ways to set or get the default time zone settings, for example, using the Date_default_timezone_setl function to set the time zone

The code is as follows Copy Code

Date_default_timezone_set ("Asia/shanghain"); Set the time zone to Shanghai
?>

or set the Tokyo time zone code to:

Date_default_timezone_set ("Asia/tokyo");
?>

When the system is initialized, add

Ini_set (' Date.timezone ', ' Asia/shanghai ');

Or

Date_default_timezone_set ("PRC");

Will resolve the time zone difference 8


And if you have php.ini administrative privileges can be modified directly in the php.ini OH


To manually modify php.ini settings

Open php found Date.timezone = "PRC" If you have to remove the previous semicolon, no words manually added!

After loading the PHP5 you will find this problem:

The code is as follows Copy Code
$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 not the same as your current time.

The reason is that if you do not set your server local time zone in the program or configuration file, the time taken by PHP is Greenwich Mean time, so there will be a discrepancy with your local time.

Greenwich Mean Time and Beijing time is about 8 hours. So how do we avoid time errors?

Let's take a look at the workaround:

In the header, use Date_default_timezone_set () to set my default time zone to Beijing time.

The code is as follows Copy Code


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.

Make sure that the H in date (' y-m-d h:i:s ') is uppercase if an Insert database error occurs.

http://www.bkjia.com/PHPjc/631528.html www.bkjia.com true http://www.bkjia.com/PHPjc/631528.html techarticle PHP Default time zone is Europe and the United States, so with our Chinese time zone a full 8 hours, oh, let me introduce you to the PHP set time zone method, there is a need to know the friends can enter the reference. ...

  • 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.