Typecho 类似微博上几分钟前、几天前
No Reply , Posted in 代码 on April 27, 2014
这样可以实现,在functions.php里添加如下代码:
function getDayAgo($date){ $d = new Typecho_Date(Typecho_Date::gmtTime()); $now = $d->format('Y-m-d H:i:s'); $t = strtotime($now) - strtotime($date); if($t < 60){ return $t . '秒前'; } if($t < 3600){ return floor($t / 60) . '分钟前'; } if($t < 86400){ return floor($t / 3670) . '小时前'; } if($t < 604800){ return floor($t / 86400) . '天前'; } if($t < 2419200){ return floor($t / 604800) . '周前'; } if($t < 31536000 ){ return floor($t / 2592000 ).'月前'; } return floor($t / 31536000 ).'年前'; }
调用方法不变,比如在post.php调用文章发布时间:
<?php $this->date('Y-m-d H:i'); ?>
by muchun @ typecho论坛