函数实现(获取刚刚、几分钟前、几小时前、几天前、几月前的时间)

代码如下(例1):

<?php
function time_tran($the_time){ $now_time = date("Y-m-d H:i:s",time()+8*60*60);//8*60*60时区的设置 $now_time = strtotime($now_time); $show_time = strtotime($the_time); $dur = $now_time - $show_time; if($dur < 0){ return $the_time; }else{ if($dur < 60){ return $dur.'秒前'; }else{ if($dur < 3600){ return floor($dur/60).'分钟前';//floor(x),有时候也写做Floor(x),其功能是“向下取整”,或者说“向下舍入”;与floor函数对应的是ceil函数,即上取整函数。 }else{ if($dur < 86400){ return floor($dur/3600).'小时前'; }else{ if($dur < 259200){//3天内 return floor($dur/86400).'天前'; }else{ return $the_time; } } }

代码如下(例2):

<?php
function format_date($time){
    $t=time()-$time;
    $f=array(
        '31536000'=>'年',
        '2592000'=>'个月',
        '604800'=>'星期',
        '86400'=>'天',
        '3600'=>'小时',
        '60'=>'分钟',
        '1'=>'秒'
    );
    foreach ($f as $k=>$v)    {
        if (0 !=$c=floor($t/(int)$k)) {
            return $c.$v.'前';
        }
    }
}
?>

  

原文地址:https://www.cnblogs.com/gaojunshan/p/6560160.html