WordPress 实现文章和评论日期时间格式为几分钟之前几小时之前

2016-09-05 06:12 1,564 11 条评论 龙笑天下
Dragon主题购买

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

Wordpress 实现文章和评论日期时间格式为几分钟之前几小时之前

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

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

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

WordPress 时间日期函数(最全最正确版):the_time()与 get_the_time()-BG
WordPress 时间日期函数(最全最正确版):the_time()与 get_the_time()

WordPress 时间日期函数(最全最正确版):the_time()与 get_the_time()

在修改的过程中,我们难免会用到时间函数,这当然就涉及到了 WordPress 的时间格式。可能很多朋友对于时间的输出表现有着比较特殊的要求,那么我们只要对时间函数: the_time() 做一...

将下边的代码丢到 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 计算几分钟前、几小时前、几天前的几个函数、类分享

「点点赞赏,手留余香」

还没有人赞赏,快来当第一个赞赏的人吧!

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

2016-08-22

2016-09-13

发表评论

表情 格式 贴图 链接 私密 签到
Dragon主题购买阿里云特价云服务器1核2G低至86元,N4共享型服务器3年仅需799元腾讯云特价云服务器1核2G 88元/年 2核4G3M688元/3年,更有千元代金券礼包免费领!
评论
正在努力加载中...
扫一扫二维码分享
×
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies. Learn more