2016-07-10 6 views
-1

Ich weiß, diese Frage über den Unterschied von zwei Daten bekommen hat wie Dutzende Male gefragt worden, aber trotz jede Antwort Umsetzung ich finden konnte, kann ich meinen Code nicht erhalten zu arbeiten.Fehler beim Versuch, den Unterschied von zwei Daten zu bekommen

Was ich will, erreichen, ist zu den Unterschied von zwei Daten bekommen, aber ich erhalte die folgende Fehlermeldung/Warnung:

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

Mein PHP-Code:

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

Wie kann ich das beheben?

+0

'date_diff' ein Objekt vom Typ' DateTimeInterval' zurückgibt. Sie können das nicht an date_format übergeben. Siehe: http://php.net/manual/en/dateinterval.format.php Sehen Sie sich Beispiel 1 an. – ymas

Antwort

-1

Ich kann nicht den Code in der Frage kommen, zu arbeiten, so ich es mein eigenes Drehbuch über endete machen, die perfekt funktioniert.

Der PHP-Code:

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

Dies ist eine schlechte Lösung. Mit den [\ DateTime-Klassen] (http://php.net/datetime) können Sie all dies mit viel weniger Code erledigen. Sie kümmern sich auch um Schaltjahre und DST-Änderungen für Sie, was Ihr Code nicht tut. – vascowhite

+0

Ich möchte, dass das Skript ** überprüft, ob ein Benutzer 14 oder mehr Tage hat, um sich anzumelden **. Wenn das letzte Mal im Februar eines Schaltjahres eingeloggt war und jetzt März ist, wird das Skript ** auch einen 29. Tag hinzufügen **, also bist du absolut falsch **, wenn du sagst, dass Schaltjahre nicht berücksichtigt werden zum. Für größere Intervalle würde ich in der Tat einen fortgeschritteneren Code brauchen, aber für meine Zwecke wäre etwas fortgeschrittener völlig überflüssig. –

1

date_diff gibt ein Objekt DateInterval zurück, das Sie nicht mit date_format formatieren können. Rufen Sie ->format() auf der $difference stattdessen.

Statt echo date_format($difference, "%d") tun Sie eine echo $difference->format('%d').

1

Ich war auf der Suche nach etwas ähnliches vor einer Weile, das ist, was ich gefunden habe.

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