0

此函数产生的结果为 0:

function time_difference($time1, $time2) {
    $dt1 = new DateTime($time1);
    $dt2 = new DateTime($time2);

    $difference = $dt1->diff($dt2);
    var_dump($difference);
}

这是 var_dump 的输出。是的,我确实验证了 $time1 和 $time2 的日期/时间有效。

object(DateInterval)#7 (15) {
    ["y"]=> int(0)
    ["m"]=> int(0)
    ["d"]=> int(0)
    ["h"]=> int(0)               
    ["i"]=> int(0)
    ["s"]=> int(0)
    ["weekday"]=> int(0)
    ["weekday_behavior"]=> int(0) 
    ["first_last_day_of"]=> int(0)
    ["invert"]=> int(0)
    ["days"]=> int(0)
    ["special_type"]=> int(0)
    ["special_amount"]=> int(0)
    ["have_weekday_relative"]=> int(0) 
    ["have_special_relative"]=> int(0)
}

任何帮助表示赞赏。

4

1 回答 1

0

看起来您正试图$dt1从 DateTime 对象构造一个 DateTime 对象$time1DateTime 构造函数需要一个字符串。如果您将 DateTime 对象传递给函数,请尝试以下操作:

function time_difference($time1, $time2) {
    $difference = $time1->diff($time2);
    var_dump($difference);
}
于 2013-11-13T00:09:18.880 回答