简体   繁体   English

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

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

I'm trying to convert MysQl timestamp to display time ago but I get this error 我正在尝试将MysQl时间戳转换为显示time ago但出现此错误

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (1493324845) 致命错误:消息为'DateTime :: __ construct()的未捕获异常'Exception':无法解析时间字符串(1493324845)

script 脚本

<?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 Timestamp format is 2017-04-27 22:27:25 MysQl时间戳格式为2017-04-27 22:27:25

What am I doing wrong? 我究竟做错了什么?

I was getting the error because I was trying to parse string where date_diff() expects datetime object. 我收到错误消息是因为我试图解析date_diff()需要datetime对象的字符串。 This works for me: 这对我有用:

   <?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