Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2009
    Location
    Bangalore
    Posts
    75
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question Problem in getting time difference

    Hi All,

    I need to find the time difference in the format like :

    $First_time = '18:20:25';
    $Second_time = '12:00:00';
    $diff = '6:20:25';

    can anyone help me in getting such result.

    Thanx in advance ....

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    I found the script (below) using Google, but didn't test it:

    PHP Code:
    function getMytimeDiff($t1,$t2)
    {
    $a1 explode(":",$t1);
    $a2 explode(":",$t2);
    $time1 = (($a1[0]*60*60)+($a1[1]*60)+($a1[2]));
    $time2 = (($a2[0]*60*60)+($a2[1]*60)+($a2[2]));
    $diff abs($time1-$time2);
    $hours floor($diff/(60*60));
    $mins floor(($diff-($hours*60*60))/(60));
    $secs floor(($diff-(($hours*60*60)+($mins*60))));
    $result $hours.":".$mins.":".$secs;
    return 
    $result;
    }
    $mytime1 "14:05:08";
    $mytime2 "03:22:54";
    $cool getMytimeDiff($mytime1,$mytime2); 

  • #3
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    I tested that function and it does indeed work. I also tried to find the difference like this:

    PHP Code:
    $time1 '18:20:25';
    $time2 '12:00:00';
    $diff strtotime($time1) - strtotime($time2); 
    I thought I would have an edge, but the already provided function is about 5 times faster using the math and exploding.


  •  

    Posting Permissions

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