PHP calculates the number of days, hours, minutes, seconds of two time difference
PHP calculates two time difference days, hours, minutes, seconds is not as convenient as other languages, but it is easy to find out the way of time in PHP. This article explains how PHP calculates the time difference. Need the Code farmers can refer to.
Let's look at the following example on the Internet:
$startdate is the start time, $enddate is the end time
<?php
$startdate = "2011-3-15 11:50:00";
$enddate = "2012-12-12 12:12:12";
$date =floor ((Strtotime ($enddate)-strtotime ($startdate))/86400);
echo "Days off:". $date. " Days <br/><br/> ";
$hour =floor ((Strtotime ($enddate)-strtotime ($startdate))%86400/3600);
echo "Hour difference:". $hour. " Hours <br/><br/> ";
$minute =floor ((Strtotime ($enddate)-strtotime ($startdate))%86400/60);
echo "Number of minutes:". $minute. " Minutes <br/><br/> ";
$second =floor ((Strtotime ($enddate)-strtotime ($startdate))%86400%60);
echo "Number of seconds:". $second. " Seconds ";
? >
Whether it's the type of time you're constructing with a string (using Strtotime conversion) or the type of time you get directly using the system's timing function, it's actually a variable of long shaping. Two such variables, it is obvious that you can do subtraction.
Do subtraction to get the value is the difference in the number of seconds, this number of seconds to 86400 (the number of seconds in a day), then get the difference. If modulo 86400 is taken, 3,600 seconds, 60 seconds, and then the number of hours and minutes are associated. If modulo 86400 is taken and 60 is modulo, the difference in seconds is obtained.
Thank you for reading, I hope to help you, thank you for your support for this site!