站三界导航
首页 TP代码tp5整理多种格式化时间戳转换多久之前显示代码

tp5整理多种格式化时间戳转换多久之前显示代码

  • TP代码
  • 来源:站三界导航
  • 94阅读
  • 2022-10-01

常用多久之前

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);
    }
}
数字没样式的代码:

/*文章发布多少时间前*/
    function TimeRule($time)
    {
        $startdate = date('Y-m-d H:i:s',$time);//时间戳转日期(要是日期的话可以不用转)
        $enddate = date('Y-m-d H:i:s');//当前日期
        $date = floor((strtotime($enddate) - strtotime($startdate)) / 86400);
        $hour = floor((strtotime($enddate) - strtotime($startdate)) % 86400 / 3600);
        $minute = floor((strtotime($enddate) - strtotime($startdate)) % 86400 % 3600 / 60);
        $second = floor((strtotime($enddate) - strtotime($startdate)) % 86400 % 60);
        if ($date > 90)
        {
            return '很久前';
        }
        elseif ($date >= 30 && $date <= 90)
        {
            return floor($date / 30) . '个月前';
        }
        elseif ($date > 0 && $date < 30)
        {
            return $date . '天前';
        }
        elseif ($hour < 24 && $hour > 0)
        {
            return $hour . '小时前';
        }
        elseif ($minute < 60 && $minute > 0)
        {
            return $minute . '分钟前';
        }
        elseif ($second < 60 && $second > 0)
        {
            return $second . '秒前';
        }
    }
数字有样式代码:

/*文章发布多少时间前*/
    function TimeRule($time)
    {
        $startdate = date('Y-m-d H:i:s',$time);//时间戳转日期(要是日期的话可以不用转)
        $enddate = date('Y-m-d H:i:s');//当前日期
        $date = floor((strtotime($enddate) - strtotime($startdate)) / 86400);
        $hour = floor((strtotime($enddate) - strtotime($startdate)) % 86400 / 3600);
        $minute = floor((strtotime($enddate) - strtotime($startdate)) % 86400 % 3600 / 60);
        $second = floor((strtotime($enddate) - strtotime($startdate)) % 86400 % 60);
        if ($date > 90)
        {
            return "<span style='color: red;'>很久之前</span>";
        }
        elseif ($date >= 30 && $date <= 90)
        {
            return "<p><span style='color: red;'>".floor($date / 30)."</span>个月前</p >";
        }
        elseif ($date > 0 && $date < 30)
        {
            return "<p><span style='color: red;'>".$date."</span>天前</p >";
        }
        elseif ($hour < 24 && $hour > 0)
        {
            return "<p><span style='color: red;'>".$hour."</span>小时前</p >";
        }
        elseif ($minute < 60 && $minute > 0)
        {
            return "<p><span style='color: red;'>".$minute."</span>分钟前</p >";
        }
        elseif ($second < 60 && $second > 0)
        {
            return "<p><span style='color: red;'>".$second."</span>秒前</p >";
        }
    }
在控制器中调用公共函数:

public function timeShow()
{
   $startdate = "2017-6-30 7:40:00";
   
   $t = TimeRule($startdate);
   echo $t;
}
在前台中调用公共函数:

//tp5.0
<td>{$v.add_time | TimeRule}</td>
	     传参     |   方法
//tp5.1  raw(tp5.1手册使用函数中有解释)
<td>{$v.add_time | TimeRule | raw}</td>
	     传参     |   方法   | raw 不使用(默认)转义

本文结束
本文来自投稿,不代表站三界导航立场,如若转载,请注明出处:https://www.zhansanjie.com/article/details/10844.html

版权声明:

1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

2、本站仅提供信息发布平台,不承担相关法律责任。

3、若侵犯您的版权或隐私,请联系本站管理员删除。

4、本文由会员转载自互联网,如果您是文章原创作者,请联系本站注明您的版权信息。

分享
站三界导航
本站声明:本站严格遵守国家相关法律规定,非正规网站一概不予收录。本站所有资料取之于互联网,任何公司或个人参考使用本资料请自辨真伪、后果自负,站三界导航不承担任何责任。在此特别感谢您对站三界导航的支持与厚爱。