Calculate Difference Between 2 Times in Hours in PHP

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.

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 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

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

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 calculate the hour,min between two date time?

Make use of DateTime::diff of the DateTime Class

<?php
$datetime1 = new DateTime('2014-02-11 04:04:26 AM');
$datetime2 = new DateTime('2014-02-11 05:36:56 AM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%h')." Hours ".$interval->format('%i')." Minutes";

OUTPUT :

1 Hours 32 Minutes

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



Leave a reply



Submit