Home > Article > Backend Development > How to calculate the difference between time and how many days in php
php method to calculate the difference in days between time subtraction: 1. Use the "strtotime("time")" statement to convert the two times into timestamps; 2. Subtract the two timestamps to get the time difference ;3. Divide the time difference by the total number of seconds in a day and use floor() to round. The syntax is "floor(time difference/86400)".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
How to calculate time with php Subtract the difference in days
Implementation idea:
Use strtotime() function to convert two times into timestamp
Subtract the two timestamps to get the time difference
Divide the time difference by the total number of seconds in a day (24*60*60=86400)
Use floor() to round
Implementation code:
<?php header("Content-type:text/html;charset=utf-8"); //2022年1月1日 19点30分0秒 $time1=strtotime("2022-1-1"); //2022年7月7日 7点30分0秒 $time2=strtotime("2022-7-7"); $diff_seconds = $time2 - $time1; $diff_days = floor($diff_seconds/86400); echo "两个时间相差: ".$diff_days." 天"; ?>
Recommended study: " PHP video tutorial》
The above is the detailed content of How to calculate the difference between time and how many days in php. For more information, please follow other related articles on the PHP Chinese website!