wp/wordpress网站时间修改为分钟前/小时前/几天前格式
七娃博客899人阅读
不想让自己的博客站的文章/评论发布时间用系统自带的格式?那么就来试试全站时间统一修改格式为:分钟前/小时前/几天前...,想要实现这个效果,很简单,只需我们在functions.php添加一个时间过滤器就行了!
1.functions.php里添加以下代码:
function timeago( $ptime ) { $ptime = strtotime($ptime); $etime = time() - $ptime; if($etime < 1) return '刚刚'; if($etime > 30 * 24 * 60 * 60) return date('m-d', $ptime); $interval = array ( 12 * 30 * 24 * 60 * 60 => '年前', 30 * 24 * 60 * 60 => '月前', 7 * 24 * 60 * 60 => '周前', 24 * 60 * 60 => '天前', 60 * 60 => '小时前', 60 => '分钟前', 1 => '秒前' ); foreach ($interval as $secs => $str) { $d = $etime / $secs; if ($d >= 1) { $r = round($d); return $r . $str; } }; }
2.在调用处调用:
<?php echo timeago( get_gmt_from_date(get_the_time('Y-m-d G:i:s')) ) ?>
这样你的网站的时间也就如下图一样了!
评论 | 0 条评论
登录之后才可留言,前往登录