The difference between date and gmdate functions in php

黄舟
Release: 2023-03-03 19:06:01
Original
1319 people have browsed it

There are two functions for formatting time in PHP: date() and gmdate(). The description in the official documentation is:
date -- Format a local time/date
gmdate -- Format a GMT/ UTC date/time, returned is Greenwich Mean Time (GMT).
For example, our current time zone is +8, then the time returned by the server running the following script should be like this:
The current time is assumed to be 2009-01-04 12:15:27
echo date('Y-m-d H:i: s', time()); The output is: 2009-01-04 12:15:27
echo gmdate('Y-m-d H:i:s', time()); The output is: 2009-01-04 04:15 :27
But this is only the result of running PHP under Linux+Apache. If it is run under Windows, the two functions return: 2009-01-04 04:15:27.
So, we should give a compatible writing method, use gmdate uniformly, and set the current time zone manually. The writing method is improved as follows:
echo gmdate('Y-m-d H:i:s', time() + 3600 * 8);
Like this Correct results are obtained regardless of whether it is under Linux+Apache or Windows. Of course, there is another advantage to writing this way. When the website is for the whole world, the website user only needs to set the time zone, and the program will automatically proceed according to the time zone set by the user. Time calculation, the information release time in the database only stores the time generated by the current time(), then the release time seen in China +8 time zone is: 2009-01-04 12:15:27, then in Europe +2 time zone The release time when the user sees this information is: 2009-01-04 06:15:27, so that the time of the information is all correct. Please indicate the source for reprinting: The difference between date and gmdate functions in php

The above is in php For more information on the difference between date and gmdate functions, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!