首页 > php教程 > php手册 > 正文

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

WBOY
发布: 2016-06-13 10:17:39
原创
2161 人浏览过

一分钟之内发布的,显示“刚刚”
一小时之内发布的,显示“XX分钟前”
一个月之内发布的,显示“XX天前”
一个月外的直接显示日期。

代码收藏:

<span //</span><span 发表于几分钟前</span>
<span function</span> kele_time_since(<span $older_date</span>, <span $newer_date</span> = <span false</span><span )
{
    </span><span $chunks</span> = <span array</span><span (
        </span><span array</span>(60 * 60 * 24 * 365 , '年'),
        <span array</span>(60 * 60 * 24 * 30 , '月'),
        <span //</span><span array(60 * 60 * 24 * 7, '周'),</span>
        <span array</span>(60 * 60 * 24 , '天'),
        <span array</span>(60 * 60 , '小时'),
        <span array</span>(60 , '分钟'),<span 
    );
 
    </span><span $newer_date</span> = (<span $newer_date</span> == <span false</span>) ? (<span time</span>()+(60*60*get_settings("gmt_offset"))) : <span $newer_date</span><span ;
    </span><span $since</span> = <span $newer_date</span> - <span abs</span>(<span strtotime</span>(<span $older_date</span><span ));
 
    </span><span //</span><span 根据自己的需要调整时间段,下面的24则表示小时,根据需要调整吧</span>
    <span $chunks_count</span>=<span count</span>(<span $chunks</span><span );
    </span><span if</span>(<span $since</span> < 60 * 60 * 24*30<span ){
        </span><span for</span> (<span $i</span> = 0, <span $j</span> = <span $chunks_count</span>; <span $i</span> < <span $j</span>; <span $i</span>++<span ){
            </span><span $seconds</span> = <span $chunks</span>[<span $i</span>][0<span ];
            </span><span $name</span> = <span $chunks</span>[<span $i</span>][1<span ];
 
            </span><span if</span> ((<span $count</span> = <span floor</span>(<span $since</span> / <span $seconds</span>)) != 0<span ){
                </span><span break</span><span ;
            }
        }
        </span><span $out</span> = (<span $count</span> == 1) ? '1'.<span $name</span> : "<span $count</span>{<span $name</span>}"<span ;
        </span><span if</span>(0==<span $count</span><span ){
            </span><span return</span> "刚刚"<span ;
        }
        </span><span return</span> <span $out</span>."前"<span ;
    }</span><span else</span><span {
        the_time(get_option(</span>'date_format'<span ));
    }
}</span>
登录后复制

(原文:http://www.dayecn.com/tanteng/article/show-date/)

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!