The difference between date and gmdate in PHP and the default time zone setting _php tips

Source: Internet
Author: User
Tags echo date time zones local time php script


What is the difference between date and gmdate?
There are two formatting functions in the PHP time function: Date () and Gmdate (), which are described in the official documentation as:


code as follows:
Date () #-format a local time/date
Gmdate () #-formats a GMT/UTC date/time that returns Greenwich Standard (GMT).

For example, the time zone we're in now is +8, so the server should return the following script:
Current time assumption is 2013-03-14 12:15:27
 code as follows:
echo Date (' y-m-d h:i:s '); #与date (' y-m-d h:i:s ' time ()); equivalent, output is: 2007-03-14 12:15:27
echo gmdate (' y-m-d h:i:s '); #与gmdate (' y-m-d h:i:s ' time ()); output: 2007-03-14 04:15:27

But that's just the result of running PHP under Linux+apache, and if you run it under Windows, 2 function returns are: 2013-03-14 04:15:27.
Therefore, we should give a compatibility of the writing, unified use of gmdate, and manually set the current time zone, the wording improved as follows:
Copy Code code as follows:
echo gmdate (' y-m-d h:i:s ', time () + 3600 * 8);

So no matter under the Linux+apache or Windows have the right results, of course, there is a benefit to write, when the site is facing the world, then the site users just set the time zone, the program automatically based on the time zone set by the user to calculate, When the information in the database is generated by only the current time (), then the release time in China + 8 time zone is: 2007-03-14 12:15:27, then the user in Europe + 2 time zone to see the release time of this information is: 2007-03-14 06:15:27, the time of this information is all correct.





Second, modify the default time zone PHP will affect it?


Each region has its own local time, and in the Internet as well as radio communication, the time conversion problem is particularly prominent. The whole earth is divided into 24 time zones, each of which has its own local time. In international radio or network communications, for the sake of unification, the use of a uniform time, known as Universal coordination (utc,universal time coordinated), is the global time standard set by the world standard. UTC, formerly known as Greenwich Mean Time (Gmt,greenwich Mean times), is the same as the local time in London.



The default time zone setting for PHP is UTC time, while Beijing is located in the East eight area of the time zone, leading UTC for eight hours. So in the use of PHP, such as time () to obtain the current times of the function, the time is always wrong, the performance is different from the Beijing time difference of eight hours. If you want to correctly display the Beijing time, you need to modify the default time zone settings, which can be done in the following two ways.



If you are using a stand-alone server and have permission to modify the profile, setting the time zone can be done by modifying the Date.timezone property in PHP.ini. We can set the value of this property to one of "Asia/shang", "asia/chongqing", "Etc/gmt-8″," or "PRC", and then the current time in the PHP script is Beijing time. Modify PHP's configuration file as shown below:


 code as follows:
Date.timezone = etc/gmt-8
Set the default time zone to East 8 in the configuration file (Beijing time)





If you are using Shared server/virtual host space, you do not have permissions to modify the profile php.ini, and the PHP version is more than 5.1 0, you can also call the Date_default_timezone_set () function to set the time zone before the output time. The function needs to provide a time zone identifier as a parameter and the same value as the Date.timezone property in the configuration file. The use of this function is shown below:


 code as follows:
Date_default_timezone_set (' PRC ');
Set the time zone before the output time, PRC is the People's Republic of China Echo date (' y-m-d h:i:s ', Times ());
Current time of output is




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.