检查日期时间失败


check datetime fails?

 function is_due($due_date)
        {
            $now=new DateTime('now');
            $dnow=$now->format('Y-m-d');
            $due=$due_date->format('Y-m-d');
            $interval =(strtotime($dnow)-strtotime($due));
        print_r($due_date->format('Y-m-d'));
        return $interval;
        }

我使用该函数来检查给定日期是否到期

$due_date 在从外部文件中检索为字符串 '2012-12-12' 后传入,该字符串被分配为数组元素,例如,

$datetime['due']=ReadExtFile((;当我将该函数称为print_r(is_due($datetime['due']));

$due=new DateTime($datetime['due']);

似乎什么都不起作用,我没有看到任何输出。但print_r($datetime)将显示 [到期] 的结果。

此方法的编写方式是,它期望 $due_date 是 DateTime 对象,而不是字符串。您指示您正在传入字符串而不是对象。

看起来您只想将$interval作为从给定日期到现在的计数返回。我建议只使用一种只处理字符串的简单方法。

function is_due($due_date) {
    return time() - strtotime($due_date);
    // return ((time() - strtotime($due_date)) >= 0); // depending on function you want this might work
}