簡體   English   中英

嘗試將MysQl時間戳轉換為Time Ago時無法解析時間字符串

[英]Failed to parse time string when trying to convert MysQl Timestamp to Time Ago

我正在嘗試將MysQl時間戳轉換為顯示time ago但出現此錯誤

致命錯誤:消息為'DateTime :: __ construct()的未捕獲異常'Exception':無法解析時間字符串(1493324845)

腳本

<?php 
//convert timestamp to time ago
$start_date = new DateTime();
$time = strtotime($streamuser['Datetime']);
$dbDate = new DateTime($time);
$currDate = new DateTime();
$interval = $currDate->diff($dbDate);
?>
<span><?php  echo $interval->d." days ".$interval->h." hours";?></span>

MysQl時間戳格式為2017-04-27 22:27:25

我究竟做錯了什么?

我收到錯誤消息是因為我試圖解析date_diff()需要datetime對象的字符串。 這對我有用:

   <?php 
    //convert timestamp to time ago
    $dbDate =$streamuser['Datetime'];
    $date = new DateTime($dbDate);
    $now = new DateTime();
   ?>
   <span style="font-size:smaller;">
   <?php  echo $date->diff($now)->format("%d days, %h hours and %i minutes");?> ago</span>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM