可说欢喜
Typecho

typecho 文章发布时间语义化显示 多少分钟多少小时前

by 温柔, 2023-02-18


移植主题maple时遇到的需要实现的功能 分享一下代码为typecho生态添砖加瓦

functions.php 文件添加下面代码
代码如下:

function t_ago($v) {
    $t = time() - $v;
    if ($t < 60) {
        return $t . ' seconds ago';//文字可以自己修改想要的 秒前
    } elseif ($t < 3600) {
        return floor($t / 60) .' minutes ago';//分钟前
    } elseif ($t < 86400) {
        return floor($t / 3670) .' hours ago';//小时前
    } elseif ($t < 604800) {
        return floor($t / 86400) .' days ago';//天前
    } elseif ($t < 2419200) {
        return floor($t / 604800) .' weeks ago';//周前
    } elseif ($t < 31536000 ) {
        return floor($t / 2592000 ).' months ago';//月前
    } 
    return floor($t / 31536000 ).' years ago';//年前
}

主题调用方法:

<?php echo t_ago($this->created);?>

在需要显示时间的地方填入上面代码

最终效果:
QQ截图20230218160352.png

时间戳
温柔

作者: 温柔

1 条评论
    叶开 回复
    叶开2023-02-20 08:35

    百度了很多了之前,还是你这个看起来简洁太多!mark了!

2024 © typecho & elise