2016-09-15 33 views
-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> 

這裏是截圖: enter image description here

+1

您今天比較今天。您從不使用數據庫中的日期。 –

+0

是什麼?我怎麼這樣做?還有1 downvote?大聲笑 –

+0

'$ rowdate = new DateTime();'使用今天的日期。 – Barmar

回答

3

正如我在評論中提到,你今天比較今天,因爲你從來沒有真正從數據庫中使用的日期。用當前代表的DateTime對象覆蓋該變量。

$date = new DateTime(); 
$rowdate = $stmt->fetchColumn(); 
$rowdate = new DateTime($rowdate); 
$total = $rowdate->diff($date)->format('%a days'); 
return $total; 
+0

是的,我得到它,我不得不使用新的日期時間($ rowrate)。相當不友好downvote(idk,如果你的),但我仍然在過錯我應該兩次檢查非常愚蠢的我 –

相關問題