Time Zone setting in PHP

Source: Internet
Author: User

After installing PHP5, you will find the following problems:

<?php$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.

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

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

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.