strotime比较奇怪的行为

时间:2014-01-27 09:46:33

标签: php wordpress date

我无法理解我编写的这段代码有什么问题,以及为什么它有一种不直观的行为

if(($datav == 0) || ((strtotime($datav)) > (strtotime('01/01/2014')))) {
echo 'yes';
}

else if((strtotime($datav)) < (strtotime('01/01/2014'))) {
echo 'no';
        }

$ datav是一个日期变量,可以或不能以我写的wordpress形式设置。

这是发生了什么:如果日期没有设置(== 0)代码有效,它回应'是';如果设定日期并且在2014年1月1日之前它也有效,它回应'不';但如果设定日期并且在2014年1月1日之后它不起作用并回应'不'。 在第三种情况下,我确定我已经设定了正确的日期(2014年1月1日之后的日期),因为我回应它来检查它。

我做错了什么? 谢谢任何人。

1 个答案:

答案 0 :(得分:0)

<?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 />";
}
相关问题