我知道這個問題有關獲取兩個日期的差異已被問到像幾十倍,但儘管實現每個答案,我可以找到,我不能得到我的代碼上班。錯誤試圖獲得兩個日期的差異
我想實現的是獲得兩個日期的差異,但我得到了下面的錯誤/警告:
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'返回類型DateTimeInterval'的'的對象。你不能將它傳遞給date_format。見:http://php.net/manual/en/dateinterval.format.php看例1#。 – ymas