gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 19:03:45 26 4
gpt4 key购买 nike

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

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (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() 需要日期时间对象的字符串。这对我有用:

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

关于php - 尝试将 MysQl Timestamp 转换为 Time Ago 时无法解析时间字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43671238/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com