您的位置:首页 > 其它

根据刚刚,1分钟前,1小时前,几天前显示文章发布日期

2013-12-18 06:39 441 查看


一分钟之内发布的,显示“刚刚”

一小时之内发布的,显示“XX分钟前”

一个月之内发布的,显示“XX天前”

一个月外的直接显示日期。

代码收藏:

//发表于几分钟前
function kele_time_since($older_date, $newer_date = false)
{
$chunks = array(
array(60 * 60 * 24 * 365 , '年'),
array(60 * 60 * 24 * 30 , '月'),
//array(60 * 60 * 24 * 7, '周'),
array(60 * 60 * 24 , '天'),
array(60 * 60 , '小时'),
array(60 , '分钟'),
); // www.jbxue.com

$newer_date = ($newer_date == false) ? (time()+(60*60*get_settings("gmt_offset"))) : $newer_date;
$since = $newer_date - abs(strtotime($older_date));

//根据自己的需要调整时间段,下面的24则表示小时,根据需要调整吧
$chunks_count=count($chunks);
if($since < 60 * 60 * 24*30){
for ($i = 0, $j = $chunks_count; $i < $j; $i++){
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];

if (($count = floor($since / $seconds)) != 0){
break;
}
}
$out = ($count == 1) ? '1'.$name : "$count{$name}";
if(0==$count){
return "刚刚";
}
return $out."前";
}else{
the_time(get_option('date_format'));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐