【php基础】php常用时间函数

1·获取时间
time()
    获取当前时间,最小单位是秒
microtime()
    获取当前时间,最小单位是微妙;
    microtime()--------0.1615 1561651
    microtime(true)----1561651.1615
    
2·格式化时间
date()
    date('Y-m-d H:i:s',time());格式化时间为:年-月-日  小时:分钟:秒

w    --  星期中的第几天
z    --     年份中的第几天
......

3·显示前一天的时间
    date('Y-m-d H:i:s',(time()-24*60*60));
    或者
    date('Y-m-d H:i:s',strtotime('-1 day'));
    
4·计算时间差

[php] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. function getDifference($endDate,$startDate){  
  2.     $start = strtotime($startDate);  
  3.     $end = strtotime($endDate);  
  4.     $date=floor(($end-$start)/86400);  
  5.     $hour=floor(($end-$start)%86400/3600);  
  6.     $minute=floor(($end-$start)%86400/60);  
  7.     $second=floor(($end-$start)%86400%60);  
  8.     return array('day'=>$date,'hour'=>$hour,'minute'=>$minute,'second'=>$second);  
  9. }  


注:这段代码参考了网上的代码

posted @ 2016-12-25 16:10  天涯海角路  阅读(122)  评论(0)    收藏  举报