PHP专栏

php计算两个时间差

<?php

$startdate=strtotime("2013-3-09");

$enddate=strtotime("2013-4-05");

$days=round(($enddate-$startdate)/3600/24) ;

echo $days; //days为得到的天数;

?>



本函数实现两个unix时间戳的差,并返回两个时间戳相差的天、小时、分、秒,精确到秒,两个参数都是时间戳,虽然代码很简单,但是很实用。

function timediff($begin_time,$end_time) 

      if($begin_time < $end_time){ 
         $starttime = $begin_time; 
         $endtime = $end_time; 
      } 
      else{ 
         $starttime = $end_time; 
         $endtime = $begin_time; 
      } 
      $timediff = $endtime-$starttime; 
      $days = intval($timediff/86400); 
      $remain = $timediff%86400; 
      $hours = intval($remain/3600); 
      $remain = $remain%3600; 
      $mins = intval($remain/60); 
      $secs = $remain%60; 
      $res = array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs); 
      return $res; 
}


评论

评论(0)

后面还有条评论,点击查看>>