-1
所以我基本上有兩個日期。今天是2016-09-15和2016-09-16的MySQL。我做了一個稱爲timeAgo($ id)的函數,它從mysql獲取id並將其作爲datetime返回。這裏是功能:
public function timeAgo($id)
{
$sql = "SELECT post_date from arts where id=:id";
$stmt = $this->con->prepare($sql);
$stmt->bindParam(':id',$id);
$stmt->execute();
/* i think this is the problem? */ $date = new DateTime(date('y-m-d'));
$rowdate = $stmt->fetchColumn();
$rowdate = new DateTime();
$total = $rowdate->diff($date)->format('%a days');
return $total;
}
它應該基本上返回1天,但它實際上返回0天..? 我知道我的HTML不重要,但在這裏它是反正。
<span class="postdate"><i class="fa fa-calendar-times-o" aria-hidden="true"></i> Posted: <?=$artmanager->timeAgo($id);?></span>
您今天比較今天。您從不使用數據庫中的日期。 –
是什麼?我怎麼這樣做?還有1 downvote?大聲笑 –
'$ rowdate = new DateTime();'使用今天的日期。 – Barmar