我不明白我写的这段代码出了什么问题,也不明白为什么它有不直观的行为
if(($datav == 0) || ((strtotime($datav)) > (strtotime('01/01/2014')))) {
echo 'yes';
}
else if((strtotime($datav)) < (strtotime('01/01/2014'))) {
echo 'no';
}$datav是一个日期变量,可以在我编写的wordpress表单中设置,也可以不设置。
这就是发生的事情:如果没有设置日期(== 0),代码就会工作,它会回显'yes';如果设置了日期,并且早于2014年1月1日,它也会工作,它会回显'no';但如果设置了日期,并且在2014年1月1日之后,它就不会工作,会回显'no‘。在第三种情况下,我确定我设置了正确的日期(2014年1月1日之后的日期),因为我回显了它来检查它。
我做错了什么?谢谢大家。
发布于 2014-01-27 17:57:58
<?php
$datav = 0;
test('2014-01-01');
test('2015-01-01');
test('01/01/2013');
test('25/12/2014'); // fail because strtotime will resolve as 1970-01-01
function test($datav) {
echo "Input Date: $datav = ";
$timestamp = strtotime($datav);
if ($datav == 0 || $timestamp > strtotime('01/01/2014')) {
echo 'yes';
} else if ($timestamp < strtotime('01/01/2014')) {
echo 'no';
} else {
echo 'neither';
}
echo " - What strtotime thinks was input (".date('d-M-Y', $timestamp).")<br />";
}https://stackoverflow.com/questions/21377529
复制相似问题