php把时间转换为友好时间段,几分钟前、几小时前、几天前的简单函数代码。通过把时间格式转换为时间戳,并把当前的时间戳减去之前时间的时间戳,相减后的时间戳除以相对应的秒数得到几分钟前、几小时前、几天前的展示。
第一种:
PHP代码:common.phpfunction dateline($date){
$n = time();
$t = $n - $date;
$m = 86400*30;
if($t <= 3600){
return ceil($t/60).'分钟前';
}else if($t >3600 && $t<86400){
return ceil($t/3600).'小时前';
}else if( $t >86400 && $t
return ceil($t/86400).'天前';
}else{
return date("Y-m-d",$date);
}
}
模板调用:{$vo.addtime|dateline}
效果:
{$vo.addtime|date='Y-m-d H:s:i',###}
第二种:
PHP代码:<?php
function dateline($date){
//当前时间的时间戳
$nowtimes = time();
//之前时间参数的时间戳
$posttimes = $date;
//相差时间戳
$counttime = $nowtimes - $posttimes;
//进行时间转换
if($counttime<=10){
return '刚刚';
}else if($counttime>10 && $counttime<=30){
return '刚才';
}else if($counttime>30 && $counttime<=60){
return '刚一会';
}else if($counttime>60 && $counttime<=120){
return '1分钟前';
}else if($counttime>120 && $counttime<=180){
return '2分钟前';
}else if($counttime>180 && $counttime<3600){
return intval(($counttime/60)).'分钟前';
}else if($counttime>=3600 && $counttime<3600*24){
return intval(($counttime/3600)).'小时前';
}else if($counttime>=3600*24 && $counttime<3600*24*2){
return '昨天';
}else if($counttime>=3600*24*2 && $counttime<3600*24*3){
return '前天';
}else if($counttime>=3600*24*3 && $counttime<=3600*24*30){
return intval(($counttime/(3600*24))).'天前';
}else{
return date("Y-m-d",$posttimes);
}
}
?>