May
23
2013
php实现多少秒前,多少分钟前,多少小时前
作者:
绝缘体.. 发布:
2013-05-23 11:10 分类:
未分类 阅读:
抢沙发
现在很多网站将时间的显示都变得比较人性化,例如:某某6秒之前发表,某某在1分钟之前执行过 XX 动作等等。最近在做相关的东西于是乎也顺便自己写了一个,较为简单,仅供参考。
<?php
/*
*function:显示某一个时间相当于当前时间在多少秒前,多少分钟前,多少小时前
*timeInt:unix time时间戳
*format:时间显示格式
*/
function timeFormat($timeInt,$format='Y-m-d H:i:s'){
if(empty($timeInt)||!is_numeric($timeInt)||!$timeInt){
return '';
}
$d=time()-$timeInt;
if($d<0){
return '';
}else{
if($d<60){
return $d.'秒前';
}else{
if($d<3600){
return floor($d/60).'分钟前';
}else{
if($d<86400){
return floor($d/3600).'小时前';
}else{
if($d<259200){//3天内
return floor($d/86400).'天前';
}else{
return date($format,$timeInt);
}
}
}
}
}
}
另外还可参考:php获取时间间隔的方法总结,php显示论坛发帖时间间隔方法大全
微信扫一扫,打赏作者吧~