Home  >  Article  >  Backend Development  >  How to calculate the difference between two timestamps in php

How to calculate the difference between two timestamps in php

青灯夜游
青灯夜游Original
2022-02-18 14:41:035572browse

php method to calculate how many days the difference between two timestamps is: 1. Subtract the two timestamps to get the time difference; 2. Divide the time difference by the total number of seconds in a day "86400", and use floor( ) function, syntax "floor(time difference/86400)".

How to calculate the difference between two timestamps in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php calculates two timestamps How many days apart

Implementation idea:

  • Subtract the two timestamps to get the time difference

  • The time difference is divided by the total number of seconds in a day (24*60*60=86400)

Implementation code:

<?php
header("Content-type:text/html;charset=utf-8");
ini_set(&#39;date.timezone&#39;, &#39;Asia/Shanghai&#39;);
//2021年1月1日19点30分0秒
$time1="1609500600";
//2021年7月7日7点30分0秒
$time2="1625614200";
$diff_seconds = $time2 - $time1;
$diff_days = floor($diff_seconds/86400);
echo "两个时间戳相差: ".$diff_days." 天";
?>

How to calculate the difference between two timestamps in php

Recommended learning: "PHP video tutorial"

The above is the detailed content of How to calculate the difference between two timestamps in php. 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