php人性化时间(类似微博)转换两组
本文发布于 12 年前, 内容可能已经过时或失效!
文章目录
php时间转换函数(把时间显示人性化)
1.很详细
// 严格按照离当前时间的间隔来输出
function formatTime($time)
{
$t = time() - $time;
$f = array(
'31536000' => '年',
'2592000' => '个月',
'604800' => '星期',
'86400' => '天',
'3600' => '小时',
'60' => '分钟',
'1' => '秒'
);
foreach ($f as $k => $v) {
if (0 != $c = floor($t / (int)$k)) {
$m = floor($t % $k);
foreach ($f as $x => $y) {
if (0 != $r = floor($m / (int)$x)) {
return $c.$v.$r.$y.'前';
}
}
return $c.$v.'前';
}
}
}
//实例
$times=time()-254;
echo formatTime($times);
2.到前天
//时间转换函数(把时间显示人性化)
function formatTime($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 formatTime($times);