Home > Backend Development > PHP Tutorial > 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

不言
Release: 2023-04-03 17:24:01
Original
1883 people have browsed it

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template