您的位置:首页 > 职场人生

根据时间戳测算指定时间与当前时间的时间差(在某面试中看到的代码,,,学习下)

2016-11-03 23:33 260 查看
<?php  

//No. 1

//开始写代码,需求:

function time_tranx($the_time){         
   $now_time = 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).'分钟前';  

     }else{  

      if($dur < 86400){  

     return floor($dur/3600).'小时前';  

      }else{  

      return floor($dur/86400).'天前';

      }  

     }  

   }  

 }

 //end_code

}  

echo time_tranx("2016-11-2 10:22:01");  
?>

1.strtotime() :函数将任何英文文本的日期或时间描述解析为 Unix 时间戳    

例:strtotime("now");

strtotime("15 october 1980");

strtotime("+1
day ");

strtotime("+2
week 3 day ");

strtotime("next
day");

2.floor()
:去尾法取整

ceil():
进一法取整
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: