Deval Khandelwal
Deval Khandelwal

Reputation: 3548

Calculate difference between 2 times in hours in PHP

I have two times - For eg- the current time - 08:24 and date is 02/01/2013 in dd/mm/yyyy format and I have another time at 13:46 and date is 31/12/2012 . So, how can I calculate the difference between the 2 times in hours using PHP. (i.e. 42.63 hours) Thanks in advance.

Upvotes: 4

Views: 38583

Answers (8)

Mukesh Khatri
Mukesh Khatri

Reputation: 73

Following code is useful to get time difference with format H:I:S:

Method 1 :

function time_diff($startDateTime, $endDateTime) {
    $startDateTime = strtotime(str_replace('/', '-', $startDateTime));
    $endDateTime = strtotime(str_replace('/', '-', $endDateTime));
    $difference = abs($startDateTime - $endDateTime);
    $hours = floor($difference / 3600);
    $minutes = floor(($difference % 3600) / 60);
    $seconds = $difference % 60;

    return str_pad($hours, 2, '0', STR_PAD_LEFT). ":" . str_pad($minutes, 2, '0', STR_PAD_LEFT). ":" . str_pad($seconds, 2, '0', STR_PAD_LEFT);
}

Method 2 :

function time_diff($startDateTime, $endDateTime) {
    $datetime1 = new DateTime($startDateTime);
    $datetime2 = new DateTime($endDateTime);
    $interval = $datetime1->diff($datetime2);

    return $interval->format('%H:%I:%S');
}

Thank You!

Upvotes: 0

Arati
Arati

Reputation: 109

I found this is simplest way to find time difference, it always works for me

$timestamp1 = strtotime(date('Y-m-d H:i'));
$timestamp2 = strtotime("2020-04-05 18:00");

  $diff = abs($timestamp2 - $timestamp1)/(60*60);
echo $diff;

Upvotes: 0

Mohd Abdul Mujib
Mohd Abdul Mujib

Reputation: 13908

Just putting this here, for anyone who needs to find the difference between two dates/timestamps in Hours, Minutes 'AND' Seconds!!

$futureDate_raw = '12/13/2018'; // This is your finish date.
$fdate = strtotime($futureDate_raw);

$hours   = (($fdate - time()) / 3600;
$mins    = (($fdate - time()) % 3600) / 60;
$seconds = ((($fdate- time()) % 3600) % 60);

Upvotes: 0

Codemaker2015
Codemaker2015

Reputation: 1

I think the following code is useful to get an idea about how to calculate time difference using PHP

function date_diff($date_1 , $date_2 , $format) {
    $datetime1 = date_create($date_1);
    $datetime2 = date_create($date_2);

    $diff = date_diff($datetime1, $datetime2);

    return $diff->format($format);
}

The above function is useful to calculate difference between two times as well as dates. The dates are given as arguments with the output format.

The output format are given below:

// '%y Year %m Month %d Day %h Hours %i Minute %s Seconds' => 1 Year 3 Month 14 Day 11 Hours 49 Minute 36 Seconds // '%y Year %m Month %d Day' => 1 Year 3 Month 14 Days // '%m Month %d Day' => 3 Month 14 Day // '%d Day %h Hours' => 14 Day 11 Hours // '%d Day' => 14 Days // '%h Hours %i Minute %s Seconds' => 11 Hours 49 Minute 36 Seconds // '%i Minute %s Seconds' => 49 Minute 36 Seconds // '%h Hours => 11 Hours // '%a Days

Upvotes: 0

Pooja Yadav
Pooja Yadav

Reputation: 91

I got a simple solution, Try this one -

echo getTimeDiff("10:30","11:10");

function getTimeDiff($dtime,$atime)
    {
        $nextDay = $dtime>$atime?1:0;
        $dep = explode(':',$dtime);
        $arr = explode(':',$atime);
        $diff = abs(mktime($dep[0],$dep[1],0,date('n'),date('j'),date('y'))-mktime($arr[0],$arr[1],0,date('n'),date('j')+$nextDay,date('y')));
        $hours = floor($diff/(60*60));
        $mins = floor(($diff-($hours*60*60))/(60));
        $secs = floor(($diff-(($hours*60*60)+($mins*60))));
        if(strlen($hours)<2){$hours="0".$hours;}
        if(strlen($mins)<2){$mins="0".$mins;}
        if(strlen($secs)<2){$secs="0".$secs;}
        return $hours.':'.$mins.':'.$secs;
    }

Upvotes: 3

salathe
salathe

Reputation: 51950

Another way is to use PHP's date-related classes. The example below uses DateTime::diff() to get a DateInterval object ($interval). It then uses the interval's properties to arrive at the total number of hours in the interval.

$a = DateTime::createFromFormat('H:i d/m/Y', '08:24 02/01/2013');
$b = DateTime::createFromFormat('H:i d/m/Y', '13:46 31/12/2012');

$interval = $a->diff($b);
$hours    = ($interval->days * 24) + $interval->h
          + ($interval->i / 60) + ($interval->s / 3600);

var_dump($hours); // float(42.633333333333)

Upvotes: 4

jcsanyi
jcsanyi

Reputation: 8174

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;

Upvotes: 9

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324600

If you have the dates as timestamps (use strtotime if needed), then just subtract them, optionally take the absolute value, then divide to 3600 (number of seconds in an hour). Easy ^_^

Upvotes: 2

Related Questions