我们在浏网站或论坛的时候,经常会看到此文章或评论发布于几分钟前、几小时前、几天前这样的微时间格式,那么在wordpress中显示这种格式的时间该如何实现呢?

WordPress显示几分钟几小时几天前

方法一:添加 filter 方式

WordPress默认调用的是the_time()函数,可以通过给the_time()添加一个 filter来显示微时间,在 function.php 文件中添加如下代码。

  1. function timeago(){
  2. $time_diff = current_time('timestamp') - get_the_time('U'); //计算两个日期之间的秒数差
  3. if ($time_diff <= 86400 * 7) { // 判断日期差是否在一周之内
  4. if ($time_diff <= 300) { //5分钟之内
  5. echo '刚刚'; //显示刚刚
  6. } elseif ($time_diff <= 86400) { //24小时之内
  7. echo human_time_diff(get_the_time('U'), current_time('timestamp')) . '前'; //显示几小时前
  8. } else {
  9. $days = 0;
  10. if (get_the_time('m') != current_time('m')) { // 如果不跨月
  11. $days = daysInmonth1(get_the_time('y'), get_the_time('m')); // 计算上个月总共有多少天
  12. }
  13. $s = current_time('d') + $days; // 计算天数差时要加上上个月的总天数
  14. $m = get_the_time('d');
  15. if ($m == $s - 1) {
  16. echo '昨天'; // 显示昨天
  17. } else {
  18. echo $s - $m - 1 . '天前'; //显示几天前
  19. }
  20. }
  21. } else {
  22. if (get_the_time('Y') == current_time('Y')) {
  23. echo the_time('m月d日');
  24. } else {
  25. echo the_time('Y年m月d日');
  26. }
  27. }
  28. }
  29. add_filter('the_time',timeago');

方法二:新定义函数

首先,在我们所使用主题的 functions.php 新定义一个方法。

  1. function timeago( $stime ) {
  2.     date_default_timezone_set ('ETC/GMT');
  3.     $ptimes = strtotime($stime);
  4.     $etime = time() - $ptimes;
  5.     if ($etime < 1) {
  6.         $time = '刚刚';
  7.     }elseif($etime < 60){
  8.         $time = sprintf('%s 分钟前', $etime);
  9.     } elseif ($etime < 1440) {
  10.         $time = sprintf('%s 小时前', round($etime / 60));
  11.     } else {
  12.         $time = date("Y-m-d",$ptimes);
  13.     }
  14.     return $time;
  15. }

或者

  1. function timeago( $ptime ) {
  2.     date_default_timezone_set ('ETC/GMT');
  3.     $ptimes = strtotime($ptime);
  4.     $etime = time() - $ptimes;
  5.     if($etime < 1) return '刚刚';
  6.    $interval = array (
  7.         12 * 30 * 24 * 60 * 60  =>  '年前 ('.date('Y-m-d', $ptime).')',
  8.         30 * 24 * 60 * 60       =>  '个月前 ('.date('m-d', $ptime).')',
  9.         7 * 24 * 60 * 60        =>  '周前 ('.date('m-d', $ptime).')',
  10.         24 * 60 * 60            =>  '天前',
  11.         60 * 60                 =>  '小时前',
  12.         60                      =>  '分钟前',
  13.         1                       =>  '秒前'
  14.     );
  15.     foreach ($interval as $secs => $str) {
  16.         $d = $etime / $secs;
  17.         if ($d >= 1) {
  18.             $r = round($d);
  19.             return $r . $str;
  20.         }
  21.     };
  22. }

然后,在需要显示微时间的地方调用即可。

文章发布时间格式修改使用方法:

把原先显示时间的代码改为以下代码即可:

  1. <?php echo timeago(get_gmt_from_date(get_the_time('Y-m-d G:i:s'))); ?>

评论发布时间格式修改使用方法:

把原先显示时间的代码改为以下代码即可:

  1. <?php echo timeago(get_gmt_from_date(get_comment_date('Y-m-d G:i:s'))); ?>

本文已通过「原本」原创作品认证,转载请注明文章出处及链接。

夏日阳光
  • 本文由 夏日阳光 发表于 2019年5月6日
  • 本文为夏日阳光原创文章,转载请务必保留本文链接:https://www.pieruo.com/57.html
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证
加载中...