【发布时间】:2022-01-05 15:52:51
【问题描述】:
当我将“7.12.60”传递给 strtotime 时,它会返回今天的时间戳!我知道 strtotime 猜测给定字符串的格式,但这里的值根本不正确!它只返回今天的时间戳:
echo date( 'Y-m-d', strtotime('7.12.60') ); // outputs 2022-01-05!
我期望的是收到一个错误或正确的值,而不是一个虚构的值
【问题讨论】:
-
60 可能会混淆它,预计是 4 位数的年份。如果您使用 1960,它可以正常工作。
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-), the date string is parsed as y-m-d. -
因为它将其转换为时间,而不是日期。试试
Y-m-d H:i:s,你会得到"2022-01-05 07:13:00"——第7小时,第12分钟加上60秒变成第13分钟 -
@user335870 它将其视为没有日期的时间。在这种情况下,日期假定为今天。
-
换句话说,它的行为就像值是
7:12:60 -
下一期,即使您将
.更改为-并使用DateTime和createFromFormat(),60 也将被假定为 2060 而不是 1960,因此请确保如果您以这种格式获取日期,您可能还需要在日期中添加一个世纪DEMO