WordPress代码构造函数将发布日期显示为多久前

cera cera

将日期显示为多久前可以让读者很方便的知道该信息的重要性,一条2年前发布的信息和2天前发布的信息价值是不一样的。发布时间显示为多久前比直接显示发布时间要直观得多,可以提高用户体验,具体的效果可以看看博客的评论日期显示。这个功能代码非常完善,是国内的热门主题大前端dux中使用的方法,直接复制就能用。

函数源码
function _get_time_ago($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;
}
};
}
将上面的函数代码复制到你的主题目录下functions.php文件中即可正常使用,函数中需要传入一个时间字符串,这个字符串格式只要是标准的日期格式即可,这个函数会自定将其转换为时间戳,然后计算与当前时间的差异。

使用方法
在评论列表中使用:

echo _get_time_ago($comment->comment_date);

cera cloudiplc tengxunyun

相关推荐

mjjping.com cera cera cloudiplc