尝试获取两个日期的差异时出错

Error trying to get the difference of two dates

我知道这个关于获取两个日期之差的问题已经被问过 几十次,但是尽管我实现了我能找到的每一个答案,我可以'让我的代码工作

我想要实现的是得到两个日期的差异,但我得到以下error/warning:

Warning: date_format() expects parameter 1 to be DateTimeInterface, object given.

我的PHP代码:

<?php
    // Include the function's library
    include_once "Functions.php";

    // Set default timezone_abbreviations_list
    date_default_timezone_set("Europe/Athens");

    // Establish connection to the database, pass the query and get the result
    $connection = connect("limited"); // Read-only
    $query = "SELECT `Last_Login` FROM `users`";
    $result = mysqli_query($connection, $query);

    // Initiate the variables
    $last_login = array();
    $now = date_create(date("Y-m-d h:i:s"));

    if (mysqli_num_rows($result)) {
        while ($data = mysqli_fetch_assoc($result)) {
            array_push($last_login, date_create($data["Last_Login"]));
            /* The date in the database is saved in this format : 2016-07-10 09:43:06 */
        }
    }

    for ($i = 0; $i < count($last_login); $i++) {
        $difference = date_diff($now, $last_login[$i], true) . "<br/>";
        echo date_format($difference, "%d");
    }
?>

我该如何解决这个问题?

date_diff returns 一个 DateInterval 对象,您不能使用 date_format 对其进行格式化。改为在 $difference 上调用 ->format()

而不是 echo date_format($difference, "%d") 做一个 echo $difference->format('%d').

我刚才在找类似的东西,这就是我找到的。

$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
$t = $diff->format("%a");
echo "$t";

我无法让问题中的代码起作用,所以我最终制作了自己的脚本,效果很好。

PHP代码:

<?php
    // Establish connection to the database, pass the query and get the result
    $connection = connect("limited");
    $query = "SELECT `Last_Login` FROM `users`";
    $result = mysqli_query($connection, $query);

    // Initiate variables
    $now = date_parse(date("Y-m-d H:i:s"));
    $last_login = ["Last_Login" => array()];
    $difference = ["Days" => array()];
    $entries = mysqli_num_rows($result);

    // For each entry in the database insert the date in its respective position in the arrays
    if ($entries) {
        while ($data = mysqli_fetch_assoc($result)) {
            array_push($last_login["Last_Login"], date_parse($data["Last_Login"]));
        }
    }

    // Calculate the difference between the present moment and the last login date
    for ($i = 0; $i < count($last_login["Last_Login"]); $i++) {
        // If the present year is bissextile convert months to seconds as 29 days each
        if ($now["year"] % 4 === 0) {
            if ($last_login["Last_Login"][$i]["month"] === 2) {
                $present = $now["month"] * 2505600 + $now["day"] * 86400 + $now["hour"] * 3600 + $now["minute"] * 60 + $now["second"];
                $past = $last_login["Last_Login"][$i]["month"] * 2505600 + $last_login["Last_Login"][$i]["day"] * 86400 + $last_login["Last_Login"][$i]["hour"] * 3600 + $last_login["Last_Login"][$i]["minute"] * 60 + $last_login["Last_Login"][$i]["second"];
                $difference["Days"][$i] = ($present - $past) / 86400;
            }
        }
        // If the present year is not bissextile convert months to seconds as 28 days each
        else {
            if ($last_login["Last_Login"][$i]["month"] === 2) {
                $present = $now["month"] * 2419200 + $now["day"] * 86400 + $now["hour"] * 3600 + $now["minute"] * 60 + $now["second"];
                $past = $last_login["Last_Login"][$i]["month"] * 2419200 + $last_login["Last_Login"][$i]["day"] * 86400 + $last_login["Last_Login"][$i]["hour"] * 3600 + $last_login["Last_Login"][$i]["minute"] * 60 + $last_login["Last_Login"][$i]["second"];
                $difference["Days"][$i] = ($present - $past) / 86400;
            }
        }

        // Convert months to seconds as 31 days each
        if (($last_login["Last_Login"][$i]["month"] >= 1 && $last_login["Last_Login"][$i]["month"] <= 7 && $last_login["Last_Login"][$i]["month"] % 2 === 1) || ($last_login["Last_Login"][$i]["month"] >= 8 && $last_login["Last_Login"][$i]["month"] <= 12 && $last_login["Last_Login"][$i]["month"] % 2 === 0)) {
            $present = $now["month"] * 2678400 + $now["day"] * 86400 + $now["hour"] * 3600 + $now["minute"] * 60 + $now["second"];
            $past = $last_login["Last_Login"][$i]["month"] * 2678400 + $last_login["Last_Login"][$i]["day"] * 86400 + $last_login["Last_Login"][$i]["hour"] * 3600 + $last_login["Last_Login"][$i]["minute"] * 60 + $last_login["Last_Login"][$i]["second"];
            $difference["Days"][$i] = ($present - $past) / 86400;
        }
        // Convert months to seconds as 30 days each
        elseif ($last_login["Last_Login"][$i]["month"] === 4 || $last_login["Last_Login"][$i]["month"] === 6 || $last_login["Last_Login"][$i]["month"] === 9 || $last_login["Last_Login"][$i]["month"] === 11) {
            $present = $now["month"] * 2592000 + $now["day"] * 86400 + $now["hour"] * 3600 + $now["minute"] * 60 + $now["second"];
            $past = $last_login["Last_Login"][$i]["month"] * 2592000 + $last_login["Last_Login"][$i]["day"] * 86400 + $last_login["Last_Login"][$i]["hour"] * 3600 + $last_login["Last_Login"][$i]["minute"] * 60 + $last_login["Last_Login"][$i]["second"];
            $difference["Days"][$i] = ($present - $past) / 86400;
        }
    }
?>