经常见到 几秒、几分、几小时前…… 类似的时间显示方式。比如,文章发布时间,其实就是计算文章发布时间和当前时间的差值。这个计算不难,但是 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函数:国际化 __() 获取翻译的字符串WordPress函数:国际化 __() 获取翻译的字符串
-
WordPress函数:国际化 _x() 获取具有gettext上下文的翻译字符串。WordPress函数:国际化 _x() 获取具有gettext上下文的翻译字符串。
-
WordPress函数:国际化 _ex 展示带上下文的翻译字符串WordPress函数:国际化 _ex 展示带上下文的翻译字符串
-
WordPress函数:国际化 _nx 翻译字符串,_n + _xWordPress函数:国际化 _nx 翻译字符串,_n + _x
-
WordPress函数:国际化 _nx_noop 在 pot 中注册字符串WordPress函数:国际化 _nx_noop 在 pot 中注册字符串
-
WordPress函数:translate_nooped_plural 翻译并注册字符串WordPress函数:translate_nooped_plural 翻译并注册字符串
暂无评论,抢个沙发...