欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php 人性化显示时间

程序员文章站 2022-05-31 12:25:41
...
//时间转换函数(把时间显示人性化)
function tranTime($time)
{
$rtime = date("m-d H:i",$time);
$htime = date("H:i",$time);
$time = time() - $time;
if ($time < 60)
{
$str = '刚刚';
}elseif($time < 60 * 60)
{
$min = floor($time/60);
$str = $min.'分钟前';
}elseif($time < 60 * 60 * 24)
{
$h = floor($time/(60*60));
$str = $h.'小时前 '.$htime;
}elseif($time < 60 * 60 * 24 * 3)
{
$d = floor($time/(60*60*24));
if($d==1)
$str = '昨天 '.$rtime;
else
$str = '前天 '.$rtime;
}else
{
$str = $rtime;
}
return $str;
}
$times=time()-254;
echo tranTime($times);