TP5时间戳转换为友好时间段,显示几分钟前、小时前、天前

2019-01-24   阅读:4871   分类:后端    标签: TP5

php把时间转换为友好时间段,几分钟前、几小时前、几天前的简单函数代码。通过把时间格式转换为时间戳,并把当前的时间戳减去之前时间的时间戳,相减后的时间戳除以相对应的秒数得到几分钟前、几小时前、几天前的展示。

第一种:

PHP代码:common.php

function dateline($date){
    $n = time();
    $t = $n - $date;
    $m = 86400*30;
    if($t <= 3600){
        return ceil($t/60).'分钟前';
    }else if($t >3600 && $t<86400){
        return ceil($t/3600).'小时前';
    }else if( $t >86400 && $t < $m){
        return ceil($t/86400).'天前';
    }else{
        return date("Y-m-d",$date);
    }
}


模板调用:

<span>{$vo.addtime|dateline}</span>


效果:

image.png

thinkphp模板时间调用:

{$vo.addtime|date='Y-m-d H:s:i',###}

第二种:

PHP代码:

<?php
function dateline($date){
     //当前时间的时间戳
    $nowtimes = time();
    //之前时间参数的时间戳
    $posttimes = $date;
    //相差时间戳
    $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*30){
       return intval(($counttime/(3600*24))).'天前'; 
    }else{
      return date("Y-m-d",$posttimes);
    }
}
?>


【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://www.tpxhm.com/adetail/103.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×