[PHP]
标签:
2012-10-31 12:47
在一些微博系统中经常要将时间于现在时间相比显示为多久以前发布的,如显示为:刚刚、5秒前、5小时前、5天前..
使用此函数,我们只需在前台用,实现时间友好化显示了
| /**
*
+--------------------------------------------------------------------
* Description 友好显示时间
+--------------------------------------------------------------------
* @param int $time 要格式化的时间戳 默认为当前时间
+--------------------------------------------------------------------
* @return string $text 格式化后的时间戳
+--------------------------------------------------------------------
* @author yijianqing
+--------------------------------------------------------------------
*/
function mdate($time = NULL) {
$text = '';
$time = $time === NULL || $time > time() ? time() : intval($time);
$t = time() - $time; //时间差 (秒)
if ($t == 0)
$text = '刚刚';
elseif ($t < 60)
$text = $t . '秒前'; // 一分钟内
elseif ($t < 60 * 60)
$text = floor($t / 60) . '分钟前'; //一小时内
elseif ($t < 60 * 60 * 24)
$text = floor($t / (60 * 60)) . '小时前'; // 一天内
elseif ($t < 60 * 60 * 24 * 2)
$text = '昨天 ' . date('H:i', $time); //两天内
elseif ($t < 60 * 60 * 24 * 3)
$text = '前天 ' . date('H:i', $time); // 三天内
elseif ($t < 60 * 60 * 24 * 30)
$text = date('m月d日 H:i', $time); //一个月内
elseif ($t < 60 * 60 * 24 * 365)
$text = date('m月d日', $time); //一年内
else
$text = date('Y年m月d日', $time); //一年以前
return $text;
}
|
最新评论
· · · · · ·
(共0条)
目前还没有评论