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

来源:互联网 发布:网络流行的梗 编辑:程序博客网 时间:2024/05/01 15:08

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

一小时之内发布的,显示“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'));    }}
0 0
原创粉丝点击