您的位置:首页 > 编程语言 > PHP开发

友好的显示时间 PHP端

2016-07-15 14:56 246 查看
/**
* 显示某一个时间相当于当前时间在多少秒前,多少分钟前,多少小时前, 如果超过1年, 就直接显示具体时间
*
* @param int    $inputTimestamp UnixTimestamp
* @param string $overflowTimeFormat 超过3天的时间显示格式
*
* @return string
*/
static public function timeAgo($inputTimestamp,$overflowTimeFormat = 'Y/m/d H:i')
{
if (empty($inputTimestamp) || !is_numeric($inputTimestamp) || !$inputTimestamp) {
return '';
}
$d = time() - $inputTimestamp;
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($overflowTimeFormat,$inputTimestamp);
}
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: