The difference between PHP date and gmdate

  • 2020-03-31 20:21:43
  • OfStack

Date - formats a local time/date

Gmdate - formats a GMT/UTC date/time and returns Greenwich mean time (GMT).

For example, if our time zone is +8, the time the server returns from running the following script should look like this:

The current time is assumed to be 2007-03-14 12:15:27

Echo date (" Y -m - d H: I: s', the time ()); The output is: 2007-03-14 12:15:27

Echo gmdate (' Y -m - d H: I: s', the time ()); The output is: 2007-03-14 04:15:27

However, this is only the result of running PHP under Linux+Apache. If run under Windows, both functions return: 2007-03-14 04:15:27.

Therefore, we should give a compatible writing method, unified use of gmdate, and manually set the current time zone, the writing method is improved as follows:

Echo gmdate(' y-m-d H: I :s', time() + 3600 * 8);

So no matter under the Linux + Apache or under Windows all have the right result, of course, it has a benefit, when the site is geared to the needs of the world, so the website users to set the time zone, as long as the program automatically according to the users to set time zone time calculation, database, information release time only to save the current time () generated by the time, then saw the release of time + 8 time zone in China: the 2007-03-14 12:15:27, so in Europe + 2 time zone the user sees the information release time is: In this way, the time of information is all right.

Related articles: