经常见到 几秒、几分、几小时前…… 类似的时间显示方式。比如,文章发布时间,其实就是计算文章发布时间和当前时间的差值。这个计算不难,但是 WordPress 已经提供了现成的轮子,为什么不用呢?
函数原型:
human_time_diff( int $from, int $to ): string
这个函数的作用,就是计算 $from 和 $to 两个时间戳时间的差值,返回类似 1小时,5分钟,2天之类,对阅读更加友好的时间。
使用举例:
<?php
$lastmodified = get_the_modified_time('U');
$posted = get_the_time('U');
echo "Posted " . human_time_diff($posted,current_time( 'U' )). "ago";
echo "</br>";
if ($lastmodified > $posted) {
echo "Edited " . human_time_diff($lastmodified,current_time('U')) . " ago";
}
?>
-
WordPress函数:get_template_part 用法简介get_template_part 有什么优点
-
WordPress函数:wp_is_writable 判断路径是否可写检查文件权限的方便函数
-
WordPress函数:wp_title 主题页面标题WordPress 页面标题SEO
-
WordPress函数:wp_timezone_string 返回后台设置的时区当前设置的时区是什么
-
WordPress函数:current_time 返回当前时间在 WordPress 如何获取当前时间
-
WordPress函数:set_query_var 与 get_query_var 设置与获取查询参数一对WordPress函数
暂无评论,抢个沙发...