Home  >  Article  >  Backend Development  >  The difference between date function date and gmdate in php in obtaining date

The difference between date function date and gmdate in php in obtaining date

WBOY
WBOYOriginal
2016-07-25 08:58:281062browse
  1. echo date('Y-m-d H:i:s', time()); The output is: 2013-03-14 12:15:27
  2. echo gmdate('Y-m-d H:i:s', time( )); The output is: 2013-03-14 04:15:27
Copy code

The above is the result of running PHP under Linux+Apache.

When running under Windows, the two functions return: 2013-03-14 04:15:27.

Based on compatibility considerations, we need to use gmdate uniformly and set the current time zone manually. The improved writing method is as follows:

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

With the above code, no matter under Linux+Apache or Windows All got the correct results. Another advantage of writing this way is that as long as the website user sets the time zone, the program automatically calculates the time based on the time zone set by the user. The information release time in the database only stores the time generated by the current time(). , then the release time seen by users in China +8 time zone is: 2013-03-14 12:15:27, then the release time seen by users in Europe +2 time zone is: 2013-03-14 06:15: 27. In this way, the time of the information will all correspond correctly.

I didn’t pay much attention to these two time format functions before. Today I learned the difference between date and gmdate in processing dates, and finally know how to apply them.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn