Differences between date and gmdate in PHP and default time zone settings

Source: Internet
Author: User
Tags echo date time zones
This article mainly introduces the difference between date and gmdate in PHP and the default time zone settings. For more information, see I. What is the difference between date and gmdate?

The PHP time function has two formatting functions: date () and gmdate (). the descriptions in the official documents are:

The code is as follows:

Date () #-format a local time/date gmdate () #-format a GMT/UTC date/time, and return the Greenwich Mean Time (GMT ).

For example, if the current time zone is + 8, the time returned by the server running the following script should be as follows:
The current time is assumed to be 12:15:27

The code is as follows:

Echo date ('Y-m-d H: I: s'); # equivalent to date ('Y-m-d H: I: s' time, output: 2007-03-14 12: 15: 27 echo gmdate ('Y-m-d H: I: s'); # with gmdate ('Y-m-d H: I: s 'Time (); output: 04:15:27


However, this is only the result obtained by running PHP in Linux + Apache. if it is run in Windows, both functions will return: 04:15:27.
Therefore, we should give a compatibility method, use gmdate in a unified manner, and manually set the current time zone. the improvement is as follows:

The code is as follows:

echo gmdate('Y-m-d H:i:s', time() + 3600 * 8);


In this way, both Linux + Apache and Windows get the correct results. of course, this write method also has the advantage that when the website is for the whole world, you only need to set the time zone where the website user is located, the program automatically calculates the time based on the time zone set by the user. the database only saves the time generated by the current time (). the release time displayed in the China + 8 time zone is: 12:15:27, the release time of this information displayed by users in the European + 2 time zone is: 06:15:27, so that all the information time is correct.

2. will modification to the default time zone of PHP affect?

Each region has its own local time. in online and radio communication, the time conversion problem is particularly prominent. The whole earth is divided into twenty-four time zones, each of which has its own local time. For international radio or network communications, a unified Time (UTC, Universal Time Coordinated) is used to ensure uniformity. it is a global standard Time set by the world Time standard. UTC, also known as Greenwich Mean Time (GMT, Greenwich Mean Time), is the same as local Time in London, England.

The default time zone of PHP is UTC time, while Beijing is located in the GMT + 8, eight hours ahead of UTC. So when using a function like time () in PHP to obtain the current time, the time is always incorrect, which is eight hours different from Beijing time. If you want to correctly display the Beijing time, you need to modify the default time zone settings. you can use either of the following methods.

If you are using an independent server and have the permission to modify the configuration file, you can set the time zone by modifying the date. timezone attribute in php. ini. We can set the value of this property to one of "Asia/Shang", "Asia/Chongqing", "Etc/GMT-8", or PRC, the current time obtained in the PHP script is Beijing time. Modify the PHP configuration file as follows:

The code is as follows:

Date. timezone = Etc/GMT-8 // set the default time zone in the configuration file to East 8 (Beijing time)

If you are using a shared server/virtual host space, you are not authorized to modify the configuration file php. ini, and the PHP version is later than 5.1.0. you can also call the date_default_timezone_set () function to set the time zone before the output time. This function requires a time zone identifier as a parameter, which is the same as the value of the date. timezone attribute in the configuration file. The function is used as follows:

The code is as follows:

Date_default_timezone_set ('prc'); // Set the time zone before the output time. PRC is the echo date ('Y-m-d H: I: S', time () of the People's Republic of China ()); // The current output time is Beijing time.


The above is the difference between date and gmdate in PHP and the default time zone setting _ php skills. For more information, see PHP Chinese website (www.php1.cn )!

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.