Home > Backend Development > PHP Tutorial > PHP strtotime application experience_PHP tutorial

PHP strtotime application experience_PHP tutorial

WBOY
Release: 2016-07-15 13:34:37
Original
871 people have browsed it

strtotime(date("Y-m-01 00:00:00")); // Used to get the timestamp of the first day of this month

In the actual PHP strtotime application, I suddenly encountered that the converted time was 8 hours slower than the actual time! I thought it was caused by the incorrect setting of

timezone in php.ini. After a round of inspection, I finally locked the problem on the strtotime function (problems often occur under Linux servers, and the data returned by the WINDOWS server is basically Correct)

Read the PHP manual carefully and found that the first parameter time has format requirements

time

The string to parse, according to the GNU » Date Input Formats syntax. Before PHP 5.0.0, microseconds weren't allowed in the time, since PHP 5.0.0 they are allowed but ignored.

Discovered through further follow-up on Date Input Formats

$ LC_ALL=C TZ=UTC0 date
Mon Mar 1 00:21:42 UTC 2004
$ TZ=UTC0 date +'%Y-%m-%d %H:% M:%SZ'
2004-03-01 00:21:42Z
$ date --iso-8601=ns | tr T ' ' # --iso-8601 is a GNU extension.
2004 -02-29 16:21:42,692722128-0800
$ date --rfc-2822 # a GNU extension
Sun, 29 Feb 2004 16:21:42 -0800
$ date +'% Y-%m-%d %H:%M:%S %z' # %z is a GNU extension.
2004-02-29 16:21:42 -0800
$ date +'@% s.%N' # %s and %N are GNU extensions.
@1078100502.692722128

Found that our commonly used format yyyy-mm-dd HH:ii:ss does not meet the requirements. After a rough look, I decided to use UTC0 format and then updated the above code to the following code
strtotime(date("Y-m-01 00:00:00")."Z"); // Used to get the number of this month One day timestamp
This problem is solved!

Summary of PHP strtotime application:

During the development process, we sometimes overlooked some details due to system support. As in this example, this problem will not occur under the WINDOWS platform, but the PHP strtotime application should still follow the specifications. to avoid such problems.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445988.htmlTechArticlestrtotime(date(Y-m-01 00:00:00)); // Used to get the number of this month One day timestamp suddenly encountered in the actual PHP strtotime application that the converted time was 8 hours slower than the actual time...
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