Q:

PHP code to get time difference

belongs to collection: PHP Basic Programs

0

In this code example we are taking two times and getting their differences, following code can be used to get exact time difference between two times.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Source Code and Output to get time difference

<?php
$time1 = "09:00";
$time2 = "17:00";

function get_time_difference($time1, $time2)
{
    $time1 = strtotime($time1);
    $time2 = strtotime($time2);

    if ($time2 < $time1)
    {
        $time2 += 86400;
    }

    return date("H:i", strtotime("00:00") + ($time2 - $time1));
}

$timediff = get_time_difference($time1, $time2); // 12:15:29
printf("Difference between in time : " . $timediff);
print "</br>";
?>

Output

Difference between in time : 08:00

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP code to get number of days between two dates... >>
<< How to break a foreach loop in PHP?...