php,从mysql加载timestamp并转换为time ago字符串

d6kp6zgx  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(463)

有没有可能从mysql数据库中加载一个时间戳,用当前时间戳减去它(如果需要的话),然后将它转换成一分钟前、两分钟前、一小时前等等?我需要帮助以这种格式显示时间戳。

<?php
function getuser_get($dt)
{
	$query="SELECT huffid,fullname, photourl, gender, country, city, lastupdate, dob,
		concat('[',(select GROUP_CONCAT('{".'"sportname.":"'."',sportname,'".'"}'."' SEPARATOR ',') 
		from personal_sport inner join sports on 
		personal_sport.sportid=sports.id where personal_sport.huffid=personal.huffid),']')
		as sports FROM personal 
		order by fullname asc
		limit 20;";

	return $query;
}
?>

在mysql数据库中 lastupdate 列格式如下 2018-06-21 01:05:07 ,我需要在每次刷新应用程序时动态地将其转换为“time ago”。有人能帮忙吗?先谢谢你。

92vpleto

92vpleto1#

由于它被标记为php,下面是一个使用 DateTime::diff :

$date = new DateTime('2017-06-21 01:05:07');
$diff = $date->diff((new DateTime()));
echo $diff->format('%Y years %m months %d days %H hours %i minutes %s seconds ago');

测试时:
01年0月0日09小时53分46秒前
若只显示相关信息,请参考此答案(我不会复制粘贴)

相关问题