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 4 of 4

Thread: Hours & minutes

  1. #1
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hours & minutes

    I'm selecting from a database (among other information) a time looks like this: 1170229906.
    I'm trying to find a way to print the hours & minuted differences between each couple, when I'm using while ($rs=mysql_fetch_assoc($result)) {.

    For example, if the first time is 1170229906 and the second is 1170240253 - the result should be 2:53.

    Any ideas?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    See if the script below helps out ...
    (I found it on the internet and tested it out OK).

    PHP Code:
    <?php

    $time1
    ="1170240253";
    $time2="1170229906";

    echo 
    timeDiff($time1,$time2);

    function 
    timeDiff($timestamp1,$timestamp2){

       
    $difference $timestamp1 $timestamp2;

       
    $days floor($difference/86400);
       
    $difference $difference - ($days*86400);

       
    $hours floor($difference/3600);
       
    $difference $difference - ($hours*3600);

       
    $minutes floor($difference/60);
       
    $difference $difference - ($minutes*60);

       
    $seconds $difference;
       
    //$output = "$days Days, $hours Hours, $minutes Minutes, $seconds Seconds";
       
    $output "$hours:$minutes:$seconds";

       return 
    $output;
    }
    ?>
    Output is: 2:52:27

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    You'd be better off writing your own code. This way you'll learn more from it.

    Of course you can read other people's code. But you won't learn as much when you actually write it yourself.

    The only concept you need to understand is that those numbers are just seconds. So if you subtract $time2 from $time1, you have the number of seconds between 2 times.

    Divide by 60 to get minutes, divide by 3600 to get hours. Use the remainder operator as in the previous post to get the amount of hours and then the remaining seconds.

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    I agree about learning on your own ...

    But on the other hand, I've learned most by
    looking at other scripts and modifying things,
    as well as experimenting.

    I guess each person has their own learning techniques.


  •  

    Posting Permissions

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