PHP在比较日期时出现致命错误

我在数据库中有一个日期,我想将它与当前日期进行比较。所以我写了以下函数:


$today = new DateTime();

$today_date = $current_date->format('Y-m-d H:i:s');


function do_diifernce($date_1, $date_2) {

    $my_date = $date_1;

    $createDate = new DateTime($my_date);

    $strip = $createDate->format('Y-m-d');

    $difference = $date_2->diff($createDate, true);

    $difference->total_difference = $difference->y . "." . $difference->m;

    return $difference;

}


$comparison = do_diifernce($databse_date, $today_date);

所以


$databse_date = 2019-06-01 00:00:00.000000

$today_date = 2019-05-06 10:48:01

但我无法打印$comparison.


PHP 致命错误:未捕获的错误:调用字符串上的成员函数 diff()


我该如何解决?


Cats萌萌
浏览 134回答 3
3回答

阿波罗的战车

您在此处遇到错误:$difference = $date_2->diff($createDate, true);。AFAIK,该diff()函数在 PHP 5.3 之后被弃用。如果要计算两个日期的差值,可以date_diff如下使用。<?php&nbsp; &nbsp; $date1 = date_create("2000-04-01");&nbsp; &nbsp; $date2 = date_create("2019-04-06");&nbsp; &nbsp; $diff = date_diff($date1, $date2);?>
打开App,查看更多内容
随时随地看视频慕课网APP