Home > Article > Backend Development > How to calculate the difference between two timestamps in php
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)".
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('date.timezone', 'Asia/Shanghai'); //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." 天"; ?>
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!