Differences between date and gmdate in PHP and Default Time Zone Settings

  • 2021-06-28 11:31:55
  • OfStack

1. 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:

date()   # -   Format 1 Local Time/Date 
gmdate() # -   Format 1 individual  GMT/UTC  Date/time, when Greenwich Mean Time is returned ( GMT ). 

Let's start with an example. We're now in a time zone of + 8, so the server should run the following script and return the same time:
Current time assumed to be 2013-03-14 12:15:27
echo date('Y-m-d H:i:s'); # and date('Y-m-d H:i:s' time()); Equivalent, output as: 2007-03-14 12:15:27
echo gmdate('Y-m-d H:i:s'); # and gmdate('Y-m-d H:i:s' time()); The output is: 2007-03-14 04:15:27

However, this is only the result of running PHP under Linux+Apache. If running under Windows, both functions return: 2013-03-14 04:15:27.
Therefore, we should give a compatible writing style, all 1 use gmdate, and set the current time zone manually. The writing improvements are as follows:
echo gmdate('Y-m-d H:i:s', time() + 3600 * 8);

This will get the correct results both under Linux+Apache and Windows. Of course, there is another advantage to writing this. When the website is facing the whole world, as long as the user of the website sets the time zone, the program automatically calculates the time according to the time zone set by the user. The publishing time of the information in the database only saves the time generated by the current time().Then the publishing time you see in China+8 time zone is: 2007-03-14 12:15:27, then in Europe+2 time zone users see this information publishing time is: 2007-03-14 06:15:27, so the time of information corresponds correctly.

2. Will modifying the default time zone of PHP affect it?

Each region has its own local time, and the problem of time conversion is particularly prominent in the Internet and radio communications.The globe is divided into 2104 time zones, each with its own local time.In international radio or network communication applications, for the sake of unification, a unified time, called Universal Coordination Time (UTC, Universal Time Coordinated), is a global standard time set by the World Time Standard.UTC, formerly known as Greenwich Mean Time (GMT, Greenwich Mean Time), is the same as the local time in London, England.

The default time zone setting for PHP is UTC time, while Beijing is located in the eastern 8 zone of the time zone, leading UTC by 8 hours.So when using functions like time () in PHP to get the current time, the time is always wrong, which is shown by an 8-hour difference from Beijing time.If you want the Beijing time to be displayed correctly, you need to modify the default time zone settings, which can be done in two ways.

If you are using a stand-alone server and have permission to modify the configuration file, setting the time zone can be accomplished 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 the current time obtained in the PHP script is Beijing Time.Modify the PHP configuration file as follows:

date.timezone = Etc/GMT-8
// Set the default time zone to East in the configuration file 8 District (Beijing Time) 

If you are using shared server/virtual host space, you do not have permission to modify the configuration file php.ini, and the PHP version is above 5.1.0, you can also call date_before the output timedefault_timezone_The set() function sets the time zone.The function needs to provide a time zone identifier as a parameter, with the same value as the date.timezone attribute in the configuration file.The use of this function is as follows:

date_default_timezone_set( ' PRC');          
// Set the time zone before the output time. PRC For the People's Republic of China  echo date( ' Y-m-d H:i:s', time());        
// The current time of output is Beijing Time 


Related articles: