Getting time difference between two times in PHP
You can use strtotime() for time calculation. Here is an example:
$checkTime = strtotime('09:00:59');
echo 'Check Time : '.date('H:i:s', $checkTime);
echo '<hr>';
$loginTime = strtotime('09:01:00');
$diff = $checkTime - $loginTime;
echo 'Login Time : '.date('H:i:s', $loginTime).'<br>';
echo ($diff < 0)? 'Late!' : 'Right time!'; echo '<br>';
echo 'Time diff in sec: '.abs($diff);
echo '<hr>';
$loginTime = strtotime('09:00:59');
$diff = $checkTime - $loginTime;
echo 'Login Time : '.date('H:i:s', $loginTime).'<br>';
echo ($diff < 0)? 'Late!' : 'Right time!';
echo '<hr>';
$loginTime = strtotime('09:00:00');
$diff = $checkTime - $loginTime;
echo 'Login Time : '.date('H:i:s', $loginTime).'<br>';
echo ($diff < 0)? 'Late!' : 'Right time!';
Demo
Check the already-asked question - how to get time difference in minutes:
Subtract the past-most one from the future-most one and divide by 60.
Times are done in unix format so they're just a big number showing the
number of seconds from January 1 1970 00:00:00 GMT
Calculate difference (in hours) between two times in PHP
You can try my code
<?php
$time1 = strtotime('08:00:00');
$time2 = strtotime('09:30:00');
$difference = round(abs($time2 - $time1) / 3600,2);
echo $difference;
Note: The code above will round to minutes.
PHP difference between two times
Substring at the first colon to remove the label, then use the remaining time stamp for parsing and subtracting.
Use PHP's strtotime
method to convert the times as strings into longs, then subtract the two to find the difference in seconds. You'll have a number at this point, which you can then do whatever with.
$datetime1 = substr($datetime1, strpos($datetime1, ":"));
$time1 = strtotime($datetime1);
You can then use the date
method to convert the time difference into a new string for outputting.
See:
- http://php.net/manual/en/function.substr.php
- http://php.net/manual/en/function.strtotime.php
- http://php.net/manual/en/function.strpos.php
- http://php.net/manual/en/function.date.php
How to get time difference in minutes in PHP
Subtract the past most one from the future most one and divide by 60.
Times are done in Unix format so they're just a big number showing the number of seconds from January 1, 1970, 00:00:00 GMT
how to count the difference between the two time in php and show it
You can create DateTime
instances and use diff
function to get the difference between 2 times. You can then format them in hours,minutes and seconds.
<?php
$hour1 = '12:00:00';
$hour2 = '09:00:00';
$o1 = new DateTime($hour1);
$o2 = new DateTime($hour2);
$diff = $o1->diff($o2,true); // to make the difference to be always positive.
echo $diff->format('%H:%I:%S');
Demo: https://3v4l.org/X41pv
How to get the SHORTEST difference between two times in PHP?
(This assumes your inputs are just times and not timestamps.)
If your result is greater than 12 hours, then just subtract it from 24:
if ($result > 720) {
$result = 1440 - $result;
}
You might want to change the >
to >=
depending on which side you want ties to fall on.
Calculate difference between 2 times in hours in PHP
Convert them both to timestamp values, and then subtract to get the difference in seconds.
$ts1 = strtotime(str_replace('/', '-', '02/01/2013 08:24'));
$ts2 = strtotime(str_replace('/', '-', '31/12/2012 13:46'));
$diff = abs($ts1 - $ts2) / 3600;
How do I find the hour difference between two dates in PHP?
You can convert them to timestamps and go from there:
$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);
Dividing by 3600 because there are 3600 seconds in one hour and using round()
to avoid having a lot of decimal places.
Related Topics
PHP Replacing Special Characters Like à->A, è->E
Laravel Orderby Relationship Count
Bounce Email Handling with PHP
Expected Response Code 220 But Got Code "", with Message "" in Laravel
How to Display a Date as Iso 8601 Format with PHP
What Does a Successful MySQL Delete Return? How to Check If Delete Was Successful
Title Case a String Containing One or More Last Names While Handling Names with Apostrophes
Laravel Mail::Send() Sending to Multiple to or Bcc Addresses
How to Remove Blank Lines from Text in PHP
Is There Garbage Collection in PHP
How to Send Cookies Using PHP Curl in Addition to Curlopt_Cookiefile
Increase Max_Execution_Time in PHP
Add a Prefix to Each Item of a PHP Array
PHP - Remove <Img> Tag from String
Executing a Shell Script from a PHP Script
How to Pass Variables Received in Get String Through a PHP Header Redirect