The system time zone setting for PHP date and time functions

Source: Internet
Author: User
Tags php language time zone names
Learn about time zones

The total time zone of the whole Earth is divided into 24 time zones, which are the time zone (zero zone), the East 1-12 Zone, and the West 1-12 zone. Each time zone has its own local time, and, at the same time, the local time of each time zone will be 1-23 hours apart, what we often say is that we china here is daytime, and the United States is the night. This is the time zone difference, the formation of the difference. For example, the local time in London, UK, differs from our local time in Beijing, China by 8 hours, in the field of international radio communications, using a uniform time, which we call Universal coordination time (Universal-coordinated) for short, UTC, UTC is the same as GMT (Greenwich Mean time, or GMT), and is the same as the local time in London, UK.

How to set the time zone

In the PHP5.0 time, the date () function was rewritten, so the current time is 8 hours less than the system time, in PHP, the default time is Greenwich Mean time, that is, the use of 0 time zones, we generally according to Beijing time to determine the national time, Beijing belongs to the East eight district, So to get the local current time, you must change the time zone setting for the PHP language.

In PHP, there are two ways to change the time zone :

The first type:

Modify the settings in the php.ini file, look for [data], date.timezone = option, remove the preceding quotation marks, modify to: "Date.timezone = Asia/shanghai", and then restart the Apache server

The second type:

In your application, you need to add the following function before using the time-date function

    Date_default_timezone_set (timezone);

The parameter timezone provides a recognized time zone name for PHP, and if the time zone name is not recognized, the system takes the UTC time zone. A list of each time zone names is provided in the PHP manual, which sets the time zones available in Beijing for China including PRC (Chinese People's Republic), Asia/chongqing (Chongqing), Asia/shanghai (Shanghai) or Asia/urumqi (Urumqi) , the names of the several time zones are equivalent.

After the setup is complete, the date () function will work, and there will be no more time lag problem.

Take a look at the time zone example

<?phpheader ("Content-type:text/html;charset=utf-8");    Set the Encoding echo "UTC time:". Date ("Y-m-d h:i:s"). " <br> ";d ate_default_timezone_set (" PRC "), echo" Beijing time: ". Date (" Y-m-d h:i:s ")." <br> ";? >

The result of the operation is:


If the program is uploaded to space, the php.ini file cannot be modified when the system time zone is set, only the time zone can be set using the Date_default_timezone_set () function.

Related Article

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.