WordPress 实现文章和评论日期时间格式为***天前

网站APP 8 个月前 回复

, ,

我们在很多网站和论坛都会看到此文章或评论发布于几分钟前、几小时前、几天前,这样给人感觉非常好。今天在此就将几种实现方法分享给大家。

将文章发布时间改为这种形式

WordPress 本身调用时间的函数 the_time() 只能直接调用时间,通过他的 filter,我们可以让他显示为比较科学的几天前格式。

当然,具体的时间组成格式可参考下面这篇文章

将下边的代码丢到 function.php 的最后一个 ?> 前即可。@wordpress 大学

/**
 * WordPress 实现文章和评论日期时间格式为几分钟之前几小时之前
 * https://www.ilxtx.com/time-ago.html
 */
function Bing_filter_time(){
	global $post ;
	$to = time();
	$from = get_the_time('U') ;
	$diff = (int) abs($to - $from);
	if ($diff <= 3600) {
		$mins = round($diff / 60);
		if ($mins <= 1) {
			$mins = 1;
		}
		$time = sprintf(_n('%s 分钟', '%s 分钟', $mins), $mins) . __( '前' , 'Bing' );
	}
	else if (($diff <= 86400) && ($diff > 3600)) {
		$hours = round($diff / 3600);
		if ($hours <= 1) {
			$hours = 1;
		}
		$time = sprintf(_n('%s 小时', '%s 小时', $hours), $hours) . __( '前' , 'Bing' );
	}
	elseif ($diff >= 86400) {
		$days = round($diff / 86400);
		if ($days <= 1) {
			$days = 1;
			$time = sprintf(_n('%s 天', '%s 天', $days), $days) . __( '前' , 'Bing' );
		}
		elseif( $days > 29){
			$time = get_the_time(get_option('date_format'));
		}
		else{
			$time = sprintf(_n('%s 天', '%s 天', $days), $days) . __( '前' , 'Bing' );
		}
	}
	return $time;
}
add_filter('the_time','Bing_filter_time');

上边的代码可以让 30 天内发布的文章显示为几天前,而过了 30 天即显示为正常的标准格式日期。

将文章和评论发布时间都改为这种形式

方法一

首先,在我们所使用主题的 functions.php 文件最后一个?>前中加入以下代码:改自 @boke112,文章日期时间显示效果见本站文章页。

/**
 * WordPress 实现文章和评论日期时间格式为几分钟之前几小时之前
 * https://www.ilxtx.com/time-ago.html
 */
function timeago( $ptime ) {
	date_default_timezone_set ('ETC/GMT');
    $ptime = strtotime($ptime);
    $etime = time() - $ptime;
    if($etime < 1) return '刚刚';
    $interval = array (
        12 * 30 * 24 * 60 * 60  =>  '年前 ('.date('Y-m-d', $ptime).')',
        30 * 24 * 60 * 60       =>  '个月前 ('.date('m-d', $ptime).')',
        7 * 24 * 60 * 60        =>  '周前 ('.date('m-d', $ptime).')',
        24 * 60 * 60            =>  '天前',
        60 * 60                 =>  '小时前',
        60                      =>  '分钟前',
        1                       =>  '秒前'
    );
    foreach ($interval as $secs => $str) {
        $d = $etime / $secs;
        if ($d >= 1) {
            $r = round($d);
            return $r . $str;
        }
    };
}

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

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

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

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

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

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

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

方法二

本方法可以让 30 天内发布的文章显示为几天前,而过了 30 天即显示为正常的标准格式日期。评论日期时间显示效果参见本站评论。

首先, functions.php 中加入以下代码:改自 @蜂巢's Blog

/**
 * WordPress 实现文章和评论日期时间格式为几分钟之前几小时之前
 * https://www.ilxtx.com/time-ago.html
 */
function timeago($time) {
    date_default_timezone_set ('ETC/GMT');	
    $time = strtotime($time);
    $difference = time() - $time; 
    switch ($difference) { 
    	case $difference <= '1' :
            $msg = '刚刚';
            break; 
        case $difference > '1' && $difference <= '60' :
            $msg = floor($difference) . '秒前';
            break; 
        case $difference > '60' && $difference <= '3600' :
            $msg = floor($difference / 60) . '分钟前';
            break;
         case $difference > '3600' && $difference <= '86400' :
            $msg = floor($difference / 3600) . '小时前';
            break; 
        case $difference > '86400' && $difference <= '2592000' :
            $msg = floor($difference / 86400) . '天前';
            break; 
        case $difference > '2592000':
            $msg = ''.date('Y-m-d G:i:s',$time).'';
            break;
    } 
    return $msg;
}

文章和评论发布时间格式修改使用方法参见方法一。

更多参考:
php 计算几分钟前、几小时前、几天前的几个函数、类分享

支付宝打赏微信打赏

如果此文对你有帮助,欢迎打赏作者。

发表评论

欢迎回来 (打开)

(必填)