SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Wheelersburg
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Subtract two times from each other

    Hey,


    I have two times entered into a form, a starting time and an ending time e.g, Start Time: 3:30, End Time: 5:15.

    I enter these by hand, so I can enter them like 3.3 and 5.15 or however needed, but what I need to do is minus the end time from the start time to see how long I've worked so I can then see how much I've made per hour or whatnot...

    so I would somehow need to be able to minus 3.3 from 5.15 and get the minute difference between the two(5:15-3:30, I would want to get 105 minute difference)

    I've looked up strtotime, mktime, etc, but I have no idea how to get the minutes out of unix timestamps so any help would be greatly appreciated...

    Thanks a bunch,
    Last edited by Schwalbach; Oct 25, 2005 at 17:56. Reason: changed word in title
    http://chaseschwalbach.com

  2. #2
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    NY, USA
    Posts
    712
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP's time() is measure in seconds

    so record time() at the start and record time() at the end.... that will give you the difference in seconds. To get the difference in minutes, just divide by 60.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Wheelersburg
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I would be entering both at the same time, and would have to enter them manually because like otherwise I don't know how to do it...I would be putting in the starting time, then playing a game for however long I played it, then I would get the ending time, so how would I get each seperately?


    Thanks again
    http://chaseschwalbach.com

  4. #4
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Schwalbach
    I would be entering both at the same time, and would have to enter them manually because like otherwise I don't know how to do it...I would be putting in the starting time, then playing a game for however long I played it, then I would get the ending time, so how would I get each seperately?
    Off Topic:

    You play games for a living? man, I want your job!


    Here is a full working page I whipped up for you to play with:
    PHP Code:
    <?
    $start_time 
    $_REQUEST['start_time'];
    $end_time $_REQUEST['end_time'];
    $action $_REQUEST['action'];
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <body>
    <h3>Parse Times</h3>
    <form action="<?= $_SERVER['PHP_SELF'?>">
    Enter the start time (hours:minutes) 
    <input type="text" name="start_time" value="<?= $start_time ?>" size="60"> <br/>

    Enter the end time (hours:minutes) 
    <input type="text" name="end_time" value="<?= $end_time ?>" size="60"> <br/>
    <input type="submit" value="Go!"> <br/>

    <input type="hidden" name="action" value="go">
    </form>
    <hr>

    <?
    if($action && ($action == "go")){
        list(
    $hours$minutes) = split(':'$start_time);
        
    $startTimestamp mktime($hours$minutes);
        
        list(
    $hours$minutes) = split(':'$end_time);
        
    $endTimestamp mktime($hours$minutes);
        
        
    $seconds $endTimestamp $startTimestamp;
        
    $minutes = ($seconds 60) % 60;
        
    $hours round($seconds / (60 60));
        
        echo 
    "Time passed: <b>$hours</b> hours and <b>$minutes</b> minutes";
    }
    ?>
    </body>
    </html>
    Garcia

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Wheelersburg
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow! Thanks man, I didn't expect that kinda work but that really helps! I will try it out and let you know how it goes...Thanks again!


    P.S. I play poker for a living, seems like a game to me because I enjoy doing it, but it gets to be a job because sometimes it can get pretty boring


    Thanks again
    http://chaseschwalbach.com

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Wheelersburg
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,

    I'm getting some kind of problem with it...If I put in 2:15 and 6:30, I get 4 hours and 15 minutes(good to hear!) but if I put in 2:15 and 6:10, I get 4 hrs and 55 minutes, instead of 3 hrs and 55 minutes..

    Thanks again
    http://chaseschwalbach.com

  7. #7
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    NY, USA
    Posts
    712
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Schwalbach
    Hey,

    I'm getting some kind of problem with it...If I put in 2:15 and 6:30, I get 4 hours and 15 minutes(good to hear!) but if I put in 2:15 and 6:10, I get 4 hrs and 55 minutes, instead of 3 hrs and 55 minutes..

    Thanks again
    use floor() to get the hours instead of round().

  8. #8
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cringer
    use floor() to get the hours instead of round().
    Good one, I didn't realize that.
    Garcia

  9. #9
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    Wheelersburg
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked, thanks guys
    http://chaseschwalbach.com


Bookmarks

Posting Permissions

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