尝试将MysQl时间戳转换为Time Ago时无法解析时间字符串

时间:2017-04-28 03:01:22

标签: php mysql datetime strtotime

我正在尝试将MysQl时间戳转换为显示time ago但我收到此错误

  

致命错误:带有消息的未捕获异常'异常'   'DateTime :: __ construct():无法解析时间字符串(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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我收到错误是因为我试图解析字符串,其中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>