php实现刚刚、几分钟前、几小时前、几天前效果
<?php/*
* @param $posttime 时间:2020-06-3 12:00:18
*/
function time_ago($posttime){
//当前时间的时间戳
$nowtimes = strtotime(date('Y-m-d H:i:s'),time());
//之前时间参数的时间戳
$posttimes = strtotime($posttime);
//相差时间戳
$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*20){
return intval(($counttime/(3600*24))).'天前';
}else{
return $posttime;
}
}
//调用
$posttime_1 = "2020-05-31 05:00:18";
$posttime_2 = "2020-06-2 11:00:00";
$posttime_3 = "2020-06-3 12:05:18";
$date_1 = time_ago($posttime_1);
$date_2 = time_ago($posttime_2);
$date_3 = time_ago($posttime_3);
echo "<h1 style="text-align:center;margin-top:50px;">".$date_1."</h1>";
echo "<h1 style="text-align:center;margin-top:50px;">".$date_2."</h1>";
echo "<h1 style="text-align:center;margin-top:50px;">".$date_3."</h1>";
?>
Author:TANKING
Web:https://www.likeyunba.com/
Date:2020-06-03
WeChat:face6009
以上是 php实现刚刚、几分钟前、几小时前、几天前效果 的全部内容, 来源链接: utcz.com/a/20304.html