strtotime 在不正确的日期 [英] strtotime on incorrect dates

查看:30
本文介绍了strtotime 在不正确的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 strtotime() 有点奇怪.

对于不存在的日期,它返回后一天.

$d30= strtotime("2017-06-30");echo $d30 ."\n";echo date("Y-m-d", $d30)."\n\n";//2017-06-30$d31= strtotime("2017-06-31");echo $d31 ."\n";echo date("Y-m-d", $d31)."\n\n";//2017-07-01$d32= strtotime("2017-06-32");echo $d32 ."\n";Echo date("Y-m-d", $d32);//1970-01-01

因此对于日期格式(即 DD),01-31 是有效的(因为 3 后只能跟一个 01) 尽管是月份.根据提供的月份和日期值,日期将被调整.

也在同一页的注释中找到:

<块引用>

注意:
ddDD 格式可以上溢和下溢.第 0 天表示上个月的最后一天,而溢出计数到下个月.这使得2008-08-00"相当于2008-07-31",2008-06-31"相当于2008-07-01"(六月只有30天).1

因此 06-31 有效而 06-32 无效.

此外,在用户贡献的注释部分,米雷克在 2015-04-01 01:14 发表的说明 可能有用/有趣:

<块引用>

注意:首先检查日期(dd 或 DD)的范围是 0..31,并且只有当它适合时,才会应用上溢和下溢机制.如果没有,strtotime() 只会返回 false.如果您需要无限制的上溢/下溢进行日期计算(例如 2015-01-40 到 2015-02-09),请使用 mktime() 代替.2

<小时>

1http://php.net/manual/en/datetime.formats.date.php

2http://php.net/manual/en/datetime.formats.date.php#Hcom117014

I found something odd about strtotime().

On dates that do not exist it returns the day after.

$d30= strtotime("2017-06-30");
Echo $d30 ."\n";
Echo date("Y-m-d", $d30)."\n\n";  // 2017-06-30

$d31= strtotime("2017-06-31");
Echo $d31 ."\n";
Echo date("Y-m-d", $d31)."\n\n";  // 2017-07-01

$d32= strtotime("2017-06-32");
Echo $d32 ."\n";
Echo date("Y-m-d", $d32);         // 1970-01-01

https://3v4l.org/AjMAE

I understand the last one. It returns nothing as it's an error.
But why does the second one return first of July?
Is it a meant to be functional, in case you make a mistake it will "correct you"? Or is it a true bug in strtotime()?

解决方案

If you look at the docs for strtotime() you will see the first parameter is:

time
A date/time string. Valid formats are explained in Date and Time Formats.

If you follow the link for the date and time formats and go to Date Formats you will see:

Thus for the date format (I.e. DD), 01-31 is valid (since a 3 can only be followed by a 0 or 1) despite the month. Depending on the supplied month and date value the date will be adjusted.

Also found in the notes on that same page:

Note:
It is possible to over- and underflow the dd and DD format. Day 0 means the last day of previous month, whereas overflows count into the next month. This makes "2008-08-00" equivalent to "2008-07-31" and "2008-06-31" equivalent to "2008-07-01" (June only has 30 days).1

Hence 06-31 is valid while 06-32 is invalid.

Additionally, in the User Contributed Notes section, the note by Mirek at 2015-04-01 01:14 might be useful/interesting:

Note: the day (dd or DD) is first checked for range 0..31 and only if it fits, the overflow and underflow mechanism may apply. If not, strtotime() simply returns false. If you need unlimited over/underflow for date calculations (for example 2015-01-40 to 2015-02-09), use mktime() instead.2


1http://php.net/manual/en/datetime.formats.date.php

2http://php.net/manual/en/datetime.formats.date.php#Hcom117014

这篇关于strtotime 在不正确的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆