1. 程式人生 > >計算兩時間戳相差時間

計算兩時間戳相差時間

//功能:計算兩個時間戳之間相差的日時分秒
//$begin_time  開始時間戳
//$end_time 結束時間戳
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;
}

print_r(timediff(strtotime(2016-09-12 12:00:00'),strtotime('2016-09-15 21:50:21')));