检查日期时间失败?

时间:2012-02-17 04:18:22

标签: php

 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'后传入,该字符串被指定为数组元素,例如,

$日期时间[ '由于'] = ReadExtFile(); 当我将该函数称为print_r(is_due($datetime['due']));

$due=new DateTime($datetime['due']);
似乎什么都没有用,我看不到输出。但是print_r($datetime)会显示[到期]的结果。

1 个答案:

答案 0 :(得分:0)

编写此方法的方式,它期望$ 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
}
相关问题