【问题标题】:Error trying to get the difference of two dates尝试获取两个日期的差异时出错
【发布时间】:2016-07-10 07:27:51
【问题描述】:

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

我想要实现的是获取两个日期的差异,但我收到以下错误/警告:

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");
    }
?>

我该如何解决这个问题?

【问题讨论】:

标签: php datetime date-difference dateinterval


【解决方案1】:

date_diff 返回一个DateInterval 对象,您不能使用date_format 对其进行格式化。请改为在$difference 上致电-&gt;format()

echo $difference-&gt;format('%d')代替echo date_format($difference, "%d")

【讨论】:

    【解决方案2】:

    不久前我一直在寻找类似的东西,这就是我找到的。

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

    【讨论】:

      【解决方案3】:

      我无法让问题中的代码运行,所以我最终编写了自己的脚本,它运行良好。

      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;
              }
          }
      ?>
      

      【讨论】:

      • 这是一个糟糕的解决方案。使用\DateTime classes 将用更少的代码为您完成所有这些工作。他们还将为您处理闰年和 DST 更改,而您的代码不会。
      • 我希望脚本检查用户是否有 14 天或更长时间的登录时间。如果最后一次登录是在闰年的 2 月,而现在是 3 月,那么脚本也会添加第 29 天,所以当你说绝对错误闰年不计算在内。对于更大的间隔,我确实需要更高级的代码,但对于我的目的来说,更高级的东西完全是多余的。
      猜你喜欢
      • 2020-03-22
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多