经常见到 几秒、几分、几小时前…… 类似的时间显示方式。比如,文章发布时间,其实就是计算文章发布时间和当前时间的差值。这个计算不难,但是 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函数:translate_nooped_plural 翻译并注册字符串WordPress函数:translate_nooped_plural 翻译并注册字符串
-
WordPress函数:number_format_i18n 数字国际化WordPress函数:number_format_i18n 数字国际化
-
WordPress函数:date_i18n 日期国际化WordPress函数:date_i18n 日期国际化
-
WordPress函数:esc_html_e 转义翻译的字符串并显示WordPress函数:esc_html_e 转义翻译的字符串并显示
-
WordPress函数:esc_attr_e 属性转义、翻译、显示WordPress函数:esc_attr_e 属性转义、翻译、显示
-
WordPress函数:esc_attr_x 带上下文的转义属性,翻译显示WordPress函数:esc_attr_x 带上下文的转义属性,翻译显示
暂无评论,抢个沙发...