Home  >  Article  >  Backend Development  >  PHP code to calculate the day, hour, minute and second difference between two timestamps

PHP code to calculate the day, hour, minute and second difference between two timestamps

不言
不言Original
2018-08-08 09:32:061795browse

The content of this article is about the PHP code for calculating the day, hour, minute and second difference between two timestamps. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

Function: Calculate the day, hour, minute and second difference between two timestamps

//$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;
}

Recommended related articles:

PHP7.0 and php7.1 Summary of new syntax features in

What is the difference between the include() function and require() function in php?

The above is the detailed content of PHP code to calculate the day, hour, minute and second difference between two timestamps. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn