PHP - 为什么这个日期函数没有错误
function convertDT($TS) {
$TS = strtotime($TS);
$TS -= date("Z");
$newTS = date("Y-m-d\TH:i:s\Z", $TS);
return $newTS;
}
echo "Good: ".convertDT('2010-04-20 01:23:45')."\n";
echo "Bad: ".convertDT('2010-31-20 01:23:45')."\n";
第二个日期返回:1969-12-31T23:00:00Z
为什么?应该出现这个错误吗?
function convertDT($TS) {
$TS = strtotime($TS);
$TS -= date("Z");
$newTS = date("Y-m-d\TH:i:s\Z", $TS);
return $newTS;
}
echo "Good: ".convertDT('2010-04-20 01:23:45')."\n";
echo "Bad: ".convertDT('2010-31-20 01:23:45')."\n";
The second date returns: 1969-12-31T23:00:00Z
Why? Should this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您为其提供无效时间戳时,
strtotime()
返回false
。如果您在整数上下文中使用false
,则它相当于0
,因此当您将其传递到此行时:您实际上是根据
的时间戳创建日期0 。就 UNIX 纪元而言,
0
是 1970 年 1 月 1 日,这是您获取最终结果的地方。你最好的选择是这样的:
strtotime()
returnsfalse
when you give it an invalid timestamp.false
is equivalent to0
if you use it in an integer context, so when you pass it to this line:You are effectively creating a date from a timestamp of
0
. In terms of the UNIX epoch,0
is January 1st, 1970, which is where you're getting your end result from.Your best bet would be soemthing like this:
在第二次约会时,您尝试创建 31 月和 20 日的日期。即使您颠倒了这些日期,也没有意义。可能是一个错字。
On your second date, you are trying to create a date of month 31 and of day 20. Even if you reversed these, it wouldn't make sense. Probably a typo.
阅读此 http://php.net/manual/en/function.strtotime.php< /a>
即使输入字符串无效,该函数也不会生成任何错误。
read this http://php.net/manual/en/function.strtotime.php
This function will not generate any error even if input string is not valid.