Home >Backend Development >PHP Tutorial >Optimize the time display of articles and comments in WordPress, wordpress comments_PHP tutorial
Many blogs like to use comments published "XXX minutes ago" and articles published "XXX minutes ago". Displays the time of article comments. The improved time display method not only intuitively tells readers how long it has been since this article or comment was published, but also enhances the time sense of comment replies. I like it very much because I pressed it a while ago. I have too many things at hand, and I suffer from not being able to access the Internet during the day on weekdays, so I have been procrastinating writing the style and functions of the theme bit by bit for a long time. Recently, it is my turn to comment, so I will refer to it step by step. The popular styles on the Internet are modified bit by bit to create their own comment styles and functions.
So…..
Go…..
Jiaodao Sack... The comment date and the article date call functions differently. The following takes the comment date as an example. Please adjust the article date by yourself.
Principle of improved time display
It's very simple. It uses a built-in function of WordPress to process the difference between the current time and the time when the article or comment was published. It displays X minutes, X hours, and X days since now.
This function is human_time_diff ()
Usage:
<?php human_time_diff( $from, $to ) ;?>
Description:
Determine the difference between two time stamps.
Returns the time difference between the two time variables $from and $to in human-readable format, such as "1 hour", "5 minutes", "two days".
It’s also easy to understand in English: from to to. (This sentence is very useless, haha.)
Prototype version improved
//将你的评论时间显示的函数改成如下就可以了 <?php echo human_time_diff( get_comment_time('U') , current_time('timestamp')) ;?>
All dates are calculated with time differences. Isn’t it very violent?
How to implement the junior version
Simply add a judgment, if the comment time is not more than one day, it will display XX hours ago, if it is more than one day, it will display the original date.
Is this more humane? We can’t let readers count the days before 38 days on their fingers, right? Ha ha!
Code:
<?php //计算是否超过一天 注:86400是一天的总共的秒数 60秒X60分X24小时=86400秒 //如果觉得一天不够的话,请自行计算填上。 if (current_time('timestamp') - get_comment_time('U') < 86400 ) //一天之内显示的东西 {$cmt_time = human_time_diff( get_comment_time('U') , current_time('timestamp') ) . '-ago';} //超过一天这么显示 else{$cmt_time = get_comment_date( 'Y/n/j' ).' - '.get_comment_time('','',false);}; ;?> //将你的评论时间显示的函数改成如下就可以了 <?php echo $cmt_time ;?>
Enhanced version
So can we enhance it a little more?
Why enhance?
Well, because I am a more serious person, I feel that the date displayed in Chinese is not good-looking, and it affects my layout. I like the date displayed in English, and the Chinese version of WordPress really has no dead ends (the Chinese version is really careful), if we directly If you use the human_time_diff function to output, the Chinese version of WordPress will display all the results in Chinese version XX hours and XX days ago, which is likely to affect our typesetting, and there is neither a hook nor a non-Chinese version in the human_time_diff function. parameters, so if we want to display English, there are only two ways:
Directly modify the human_time_diff function to invalidate the Chinese version. This is too violent, and the Lun family doesn’t like it if you have to modify it again after upgrading.
Rewrite your own human_time_diff function to bypass Chinese translation.
Powerfully insert the following code into function.php:
//原函数的 day hour min 都是小写的, //我把这三个词的首写字母改成大写的,即Day Hour Min 就可以避开汉化了,你懂? if ( ! function_exists( 'xz_time_diff' ) ) : function xz_time_diff( $from, $to = '' ) { if ( empty($to) ) $to = time(); $diff = (int) abs($to - $from); if ($diff <= 3600) { $mins = round($diff / 60); if ($mins <= 1) { $mins = 1; } /* translators: min=minute */ $since = sprintf(_n('%s Min', '%s Mins', $mins), $mins); } else if (($diff <= 86400) && ($diff > 3600)) { $hours = round($diff / 3600); if ($hours <= 1) { $hours = 1; } $since = sprintf(_n('%s Hour', '%s Hours', $hours), $hours); } elseif ($diff >= 86400) { $days = round($diff / 86400); if ($days <= 1) { $days = 1; } $since = sprintf(_n('%s Day', '%s Days', $days), $days); } return $since; }endif;
The time judgment code is changed to the following:
<?php //只是把计算日期差异的函数名变了而已,其他同上。 if (current_time('timestamp') - get_comment_time('U') < 86400 ) {$cmt_time = xz_time_diff( get_comment_time('U') , current_time('timestamp') ) . '-ago';} else{$cmt_time = get_comment_date( 'Y/n/j' ).' - '.get_comment_time('','',false);}; ;?> //将你的评论时间显示的函数改成如下就可以了 <?php echo $cmt_time ;?>
Show relative time of comments and articles
According to the above version, the following one should be considered an enhanced and improved version. Because to achieve the effect, you still need to add code to the theme, so it is not the final version yet, haha.
The function code is as follows:
Relative time function
if ( ! function_exists( 'xz_time' ) ) : /** * 显示文章、评论相对时间的封装函数. *作者:XiangZi http://PangBu.com/ * @param $type 类型字符串 'cmt'或'art',用于定义显示的是评论时间还是文章时间。 * @param $ago_time 数字类型 用于定义显示相对时间的时间限制 默认为86400秒即一天。 * @param $after 字符串型 显示在相对时间之后的文字,默认为 ' - ago' * @param $late 字符串型 超过时间限制后显示的项目,默认为 get_the_time('Y/n/j - H:i')或get_comment_time('Y/n/j - H:i') * @return 返回字符串(相对时间或绝对时间) */ function xz_time ( $type = 'art', $ago_time = 86400 ,$after = ' - ago' , $late = '' ) { if ( $type === 'cmt' ){ $diff = (int) abs( get_comment_time('U') - current_time('timestamp')); if ( (!$late) || $late ==''){ $late = get_comment_time('Y/n/j - H:i');}; } if ( $type === 'art' ){ $diff = (int) abs( get_the_time('U') - current_time('timestamp')); if ( (!$late) || $late ==''){$late = get_the_time('Y/n/j - H:i');}; } if ( $diff <= 3600 ) { $mins = round($diff / 60); if ($mins <= 1) { $mins = 1; } /* translators: min=minute */ $since = sprintf(_n('%s Min', '%s Mins', $mins), $mins); } else if (($diff <= 86400) && ($diff > 3600)) { $hours = round($diff / 3600); if ($hours <= 1) { $hours = 1; } $since = sprintf(_n('%s Hour', '%s Hours', $hours), $hours); } elseif ($diff >= 86400) { $days = round($diff / 86400); if ($days <= 1) { $days = 1; } $since = sprintf(_n('%s Day', '%s Days', $days), $days); }; $since .= $after ; return $diff < $ago_time ? $since : $late ; }endif;
How to use
Insert the above code into your theme’s function.php file
Then just call this function where you want to display the relative time.
The function must input at least one parameter, which is the $type type string ‘cmt’ (comment time) or ‘art’ (article time)
Example:
//最简单的调用 echo xz_time('cmt'); //一天内的输出结果: 3 Hours-ago //一天后的输出结果: 2015/12/26 - 20:01 //调用时长为2天内的相对时间,之前时间显示默认时间 echo xz_time('cmt',172800); //2天内的输出结果: 3 Hours-ago //2天后的输出结果: 2015/12/26 - 20:01 //调用时长为2天内的相对时间,相对时间之后显示 '之前的评论' echo xz_time('cmt',172800,'之前的评论'); //2天内的输出结果: 3 Hours 之前的评论 //2天后的输出结果: 2015/12/26 - 20:01 //调用时长为2天内的相对时间,之前时间显示为 年-月-日 echo xz_time('cmt',172800,'之前的评论',get_comment_time('Y-n-j')); //2天内的输出结果: 3 Hours 之前的评论 //2天后的输出结果: 2015/12/26