/*
* 动态时间显示功能
* 文章发布多少时间前
*/
function tranTime($time) {
$rtime = date("m-d H:i", $time);
$htime = date("H:i", $time);
$time = time() - $time;
if ($time < 60) {
$str = '刚刚';
}
elseif ($time < 60 * 60) {
$min = floor($time / 60);
$str = $min . ' 分钟前';
}
elseif ($time < 60 * 60 * 24) {
$h = floor($time / (60 * 60));
$str = $h . ' 小时前 ';
}
elseif ($time < 60 * 60 * 24 * 3) {
$d = floor($time / (60 * 60 * 24));
if ($d == 1)
$str = '昨天 ';
else
$str = '前天 ';
}
else {
$str = $rtime;
}
return $str;
}
[field:pubdate function="tranTime(@me)" /]
而对于文章详情页,则可以这样调用:
{dede:field.pubdate function="tranTime(@me)" /}
以上就是完整的实现过程。通过这种方式,您可以轻松地为网站上的每篇文章添加动态的时间显示效果,从而提高用户交互体验。确保所有代码正确无误地放置在相应位置后,保存并刷新页面即可看到效果。这样的改动不仅提升了界面友好度,也间接促进了SEO优化,因为更好的用户体验往往能带来更高的搜索引擎排名。