【发布时间】: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");
}
?>
我该如何解决这个问题?
【问题讨论】:
-
date_diff返回一个DateTimeInterval类型的对象。您不能将其传递给 date_format。请参阅:php.net/manual/en/dateinterval.format.php 查看示例 #1。
标签: php datetime date-difference dateinterval