2016-07-10 36 views
-1

我知道這個問題有關獲取兩個日期的差異已被問到像幾十倍,但儘管實現每個答案,我可以找到,我不能得到我的代碼上班錯誤試圖獲得兩個日期的差異

我想實現的是獲得兩個日期的差異,但我得到了下面的錯誤/警告:

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

我該如何解決這個問題?

+0

'date_diff'返回類型DateTimeInterval'的'的對象。你不能將它傳遞給date_format。見:http://php.net/manual/en/dateinterval.format.php看例1#。 – ymas

回答

-1

我無法在問題的代碼工作,所以我最終使我自己的腳本,它,它完美的作品。

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

這是一個糟糕的解決方案。使用[\ DateTime classes](http://php.net/datetime)將使用更少的代碼爲您完成所有這些工作。他們還會照顧您的閏年和夏令時變化,您的代碼不會。 – vascowhite

+0

我希望腳本**檢查用戶是否有14天或更多天登錄**。如果上一次登錄時間是閏年的2月份,現在是3月份,腳本**也會添加第29天,所以當你說閏年沒有被記錄時,你是絕對錯誤的**對於。對於更大的時間間隔,我確實需要更高級的代碼,但對於我的目的而言,更高級的代碼將完全是多餘的。 –

1

date_diff返回一個DateInterval對象,您無法使用date_format進行格式化。改爲撥打->format()代替$difference

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

1

我一直在尋找類似的東西,這是我發現的。

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