当前位置:-go»- 网站首页 / 軟件应用區 / 正文

博客程序ZBlog文章显示友好时间格式(xxx天前)完整代码

6个月前 (11-08) / 49 次围观 / 0 次吐槽

早ZBlogPHP中,我们该如何把文章的发表时间从“X年X月X日”这样的一般格式改为“XX天前”这种格式呢?下面就针对这个问题给大家分享一段可以直接放到主题里使用的方法代码。

        ZBlog大多主题的文章时间显示格式一般为“X年X月X日”(我习惯使用这样的格式,一般不用xxx天前这样的时间,这跟个人喜好有关),而经常逛博客的朋友会发现有些博客网站的文章中的文章发表时间的格式根据时间远近分别显示为为“xxx天前”或者“xxx月前”、“xxx年前”等,如“10天前”、“1月前”和“2年前”这样的友好时间格式。

ZBlog显示“XX天前”的友好时间格式代码:我们把下面的代码放在你主题文件夹下的include.php文件中

function TimeAgo( $ptime ) {
    $ptime = strtotime($ptime);
    $etime = time() - $ptime;
    if($etime < 1) return '刚刚';
    $interval = array (
        12 * 30 * 24 * 60 * 60  =>  '年前 ('.date('Y-m-d', $ptime).')',
        30 * 24 * 60 * 60       =>  '个月前 ('.date('m-d', $ptime).')',
        7 * 24 * 60 * 60        =>  '周前 ('.date('m-d', $ptime).')',
        24 * 60 * 60            =>  '天前',
        60 * 60                 =>  '小时前',
        60                      =>  '分钟前',
        1                       =>  '秒前'
    );
    foreach ($interval as $secs => $str) {
        $d = $etime / $secs;
        if ($d >= 1) {
            $r = round($d);
            return $r . $str;
        }
    };}


友好时间方法函数调用方式:

{TimeAgo($article.Time())}


最后,大家只要把上面这段调用代码放到你要显示时间的位置就可以了,比如post-multi(摘要列表)、post-istop(置顶摘要列表)等文件中。

文本摘自:zbblog.com


更新: 2023-11-08

相關 博客程序

本筆記站部分文章、圖片收集於 互聯網,作者:僅作自學筆記;請勿轉載用於 非法途逕 , 轉載注明 _ 我欣飞祥
Url : 【 http://uuucd.cn/software/1952.html
额~ 本文暂时没人评论 来添加一个吧 …

发表评论

必填

选填

选填

必填

◎请提交您的留言_感谢您的参与讨论!!

ipcooc2@gmail.com