2016-07-10 9 views
-1

のような質問が2つの日付の差分を得ることについて知っていますが、すべての回答を実装していますが、コードを取得できません仕事する2つの日付の差分を取得しようとするとエラーが発生する

私は何を達成したいことは2つの日付の違いを得ることですが、私は次のエラー/警告取得しています:

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_formatに渡すことはできません。参照:http://php.net/manual/en/dateinterval.format.php例#1を見てください。 – ymas

答えて

-1

私はそう、私は完璧に動作している、それについての私自身のスクリプトを作ってしまった、動作するように問題のコードを取得できませんでした。

PHPコード

DateTimeInterval` `タイプのオブジェクトを返すdate_diff`
<?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)を使用すると、コードを大幅に減らすことができます。また、あなたのコードにはない閏年やDSTの変更も処理されます。 – vascowhite

+0

**スクリプトに**ログインする日数が14日以上あるかどうかを確認する**。最後にログインした月が閏年の2月で、今は3月の場合、スクリプト**は29日目も**を追加するので、閏年が占められていないと言ったときはあなたは絶対に間違っています**ために。より大きな間隔では、私は実際にはより高度なコードが必要ですが、私の目的のためには、より高度なものは完全に余計なものになります。 –

1

date_diffはを使用して書式設定できないDateIntervalオブジェクトを返します。代わりに$difference->format()に電話してください。

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"; 
関連する問題