www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Time Difference Between Two Times

  1. #1
    Join Date
    Feb 2003
    Posts
    102

    resolved [RESOLVED] Time Difference Between Two Times

    Hi all,

    I'm pulling two times out of a MySQL database. The times are in the following format: HH:MM:SS (24-hour time including preleading zeros).

    I want to be able to find the difference between the two times, and show it in the same format as per the examples below:

    Example 1
    Time1 = 08:00:00 (time is 8:00am)
    Time2 = 13:40:00 (time is 1:40pm)
    TimeDifference = 05:40:00 (difference of 5hours 40minutes)

    Example 2
    Time 1 = 22:00:00 (time is 10:00pm)
    Time 2 = 04:00:00 (time is 4:00am the next morning)
    TimeDifference = 06:00:00 (difference of 6hours)

    "Time 1" will always be in the past and "Time 2" will always be in the future. As you can see, it's possible that "Time 2" will sometimes be an "earlier" time than "Time 1". In these cases, this will always mean that Time 2 is some time in the next day.

    Between the many posts I've found here and also via BigResource, I haven't yet found a solution for this! Nor have any of my newbie efforts been any good.

    If someone could show me how to do this, that would be great!


    Cheers,

    Chris

  2. #2
    Join Date
    Aug 2009
    Posts
    84
    You can maybe try code below:

    PHP Code:
    // example 1
    $time1 "08:00:00";
    $time2 "13:40:00";

    echo 
    "Time difference: ".get_time_difference($time1$time2)." hours<br/>";

    // example 2
    $time1 "22:00:00";
    $time2 "04:00:00";

    echo 
    "Time difference: ".get_time_difference($time1$time2)." hours<br/>";

    function 
    get_time_difference($time1$time2)
    {
        
    $time1 strtotime("1/1/1980 $time1");
        
    $time2 strtotime("1/1/1980 $time2");
        
        if (
    $time2 $time1)
        {
            
    $time2 $time2 86400;
        }
        
        return (
    $time2 $time1) / 3600;
        


  3. #3
    Join Date
    Nov 2008
    Posts
    2,477
    A slight modification of the above which returns the time in HH:MM:SS format:

    PHP Code:
    <?php

    function get_time_difference($time1$time2) {
        
    $time1 strtotime("1980-01-01 $time1");
        
    $time2 strtotime("1980-01-01 $time2");
        
        if (
    $time2 $time1) {
            
    $time2 += 86400;
        }
        
        return 
    date("H:i:s"strtotime("1980-01-01 00:00:00") + ($time2 $time1));
    }

    echo 
    get_time_difference("10:25:30""22:40:59"); // 12:15:29

    ?>

  4. #4
    Join Date
    Feb 2003
    Posts
    102
    Chris22 and Mindzai... THANKS VERY MUCH!!!

    Both methods freaking brilliant!

    It was this concept that I was missing...
    if ($time2 < $time1) {
    $time2 += 86400;
    }

    (crazy simple now it's in front of me)

    Cheers lads!

    Chris

  5. #5
    Join Date
    Mar 2013
    Location
    Mumbai
    Posts
    1

    Thumbs up Nice Post..!

    Nice Post..!!

    Quote Originally Posted by chris22 View Post
    You can maybe try code below:
    Nice Post..!!


    PHP Code:
    // example 1
    $time1 "08:00:00";
    $time2 "13:40:00";

    echo 
    "Time difference: ".get_time_difference($time1$time2)." hours<br/>";

    // example 2
    $time1 "22:00:00";
    $time2 "04:00:00";

    echo 
    "Time difference: ".get_time_difference($time1$time2)." hours<br/>";

    function 
    get_time_difference($time1$time2)
    {
        
    $time1 strtotime("1/1/1980 $time1");
        
    $time2 strtotime("1/1/1980 $time2");
        
        if (
    $time2 $time1)
        {
            
    $time2 $time2 86400;
        }
        
        return (
    $time2 $time1) / 3600;
        


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles