hi,欢迎访问本站!
当前位置: 首页编程开发正文

php怎么实现时间显示为几分钟前、几小时前、几天前、几月前

墨初 编程开发 582阅读

看了几个大佬的博客,他们博文的发布时间都会根据当前的时间转换显示为几分钟前,几小时前或者几天前,几月前。下面73so的博主也写了几个转换时间的函数,大家可以参考哦。

php时间转换为几分钟前,几小时前,几天前,几月前的方法

方法1:

/**
 * @name php显示几分钟前,几小前,昨天,前天,多少天前的函数
 * @param $posttime 格式化后的时间
 * 
 * @return string 
 * https://www.73so.com
 */
function time_ago($posttime)
{
    $nowtimes = strtotime(date('Y-m-d H:i:s'),time());
    $posttimes = strtotime($posttime);
    $counttime = $nowtimes - $posttimes;
    if($counttime<=10){
        return '刚刚'; 
    }else if($counttime>10 && $counttime<=30){
        return '刚才'; 
    }else if($counttime>30 && $counttime<=60){
        return '刚一会';  
    }else if($counttime>60 && $counttime<=120){
        return '1分钟前';  
    }else if($counttime>120 && $counttime<=180){
        return '2分钟前';  
    }else if($counttime>180 && $counttime<3600){
        return intval(($counttime/60)).'分钟前';   
    }else if($counttime>=3600 && $counttime<3600*24){
        return intval(($counttime/3600)).'小时前';  
    }else if($counttime>=3600*24 && $counttime<3600*24*2){
        return '昨天';  
    }else if($counttime>=3600*24*2 && $counttime<3600*24*3){
        return '前天';  
    }else if($counttime>=3600*24*3 && $counttime<=3600*24*20){ // 这里设置超过多少天后返回输入的时间
        return intval(($counttime/(3600*24))).'天前';  
    }else{
        return $posttime;  
    }
}

echo time_ago('2022/11/23 12:59:33');
// 昨天
echo time_ago('2022/11/13 12:59:33');
// 11天前

方法2:

/**
 * @name php显示几分钟前,几小前,昨天,前天,多少天前的函数
 * @param $posttime 格式化后的时间
 * 
 * @return string 
 * https://www.73so.com
 */
function Time_Ago($ptime) 
{
    $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;
        }
    };
}

echo Time_Ago('2022/11/23 12:59:33');
// 两天前
echo Time_Ago('2022/11/13 12:59:33');
// 2周前 (11-13)
echo Time_Ago('2022/01/13 12:59:33');
// 11个月前 (01-13)
echo Time_Ago('2021/01/13 12:59:33');
// 2年前 (2021-01-13)

方法3:

/**
 * @name php显示几分钟前,几小前,昨天,前天,多少天前的函数
 * @param $posttime 格式化后的时间
 * 
 * @return string 
 * https://www.73so.com
 */
function time_ago($posttime)
{
    $agoTime = strtotime($posttime);
    // 计算出当前日期时间到之前的日期时间的毫秒数,以便进行下一步的计算
    $time = time() - $agoTime;
    
    if ($time >= 31104000) { // N年前
        $num = (int)($time / 31104000);
        return $num.'年前';
    }
    if ($time >= 2592000) { // N月前
        $num = (int)($time / 2592000);
        return $num.'月前';
    }
    if ($time >= 86400) { // N天前
        $num = (int)($time / 86400);
        return $num.'天前';
    }
    if ($time >= 3600) { // N小时前
        $num = (int)($time / 3600);
        return $num.'小时前';
    }
    if ($time > 60) { // N分钟前
        $num = (int)($time / 60);
        return $num.'分钟前';
    }
    return '1分钟前';
}
echo time_ago('2022/11/23 12:59:33');
// 1天前
echo time_ago('2022/11/13 12:59:33');
// 11天前 
echo time_ago('2022/01/13 12:59:33');
// 10月前 
echo time_ago('2021/01/13 12:59:33');
// 1年前

PS:上面给出了三个自定义函数,每个函数的计算方法与得到的结果也不相同,大家可以自己测试一下,选择合自己的!

声明:无特别说明,转载请标明本文来源!
相关推荐