PHP code to get time difference

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.

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

The following PHP topics are used in the above examples:

PHP Basic Programs »



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.