#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2006
    Posts
    21
    Rep Power
    0

    Adding time to time


    I have a duration of an event let's say it's 2 hours 41 minutes.

    I now want to add 30 minutes to it.

    What's the easiest way?

    It's not a time of day, that would be easy but just hours and minutes.
  2. #2
  3. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,961
    Rep Power
    4575
    PHP Code:
    <?php

    $minutes 
    41;
    $hours 2;

    $add 30;

    $new_minutes = ($minutes $add) % 60;
    $new_hours $hours floor(($minutes $add) / 60);

    echo 
    "Previous: {$hours} hours, {$minutes} minutes.\n";
    echo 
    "Add {$add} minutes.\n";
    echo 
    "Current: {$new_hours} hours, {$new_minutes} minutes.";

    ?>
    https://3v4l.org/eYB6R
    -- Cigars, whiskey and wild, wild women. --
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2006
    Posts
    21
    Rep Power
    0
    Can you explain line 8? What is the % sign doing?
  6. #4
  7. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,274
    Rep Power
    4193
    Basically the same as above, but I'd do this:
    PHP Code:
    <?php

    $minutes 
    41;
    $hours 2;

    $add 30;

    $newDuration = ($hours 60) + $minutes $add;

    $new_hours floor($newDuration/60);
    $new_minutes $newDuration 60;

    echo 
    "Previous: {$hours} hours, {$minutes} minutes.\n";
    echo 
    "Add {$add} minutes.\n";
    echo 
    "Current: {$new_hours} hours, {$new_minutes} minutes.";
    Convert the time to minutes, add your extension, then convert it back to hours and minutes.

    Adding the extra variable saves doing the math twice and makes the process a bit clearer I think.

    % is called the modulus operator. Essentially it returns the remainder after whole division.

    Comments on this post

    • Sepodati agrees
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2006
    Posts
    21
    Rep Power
    0
    Wow, such an easy solution. Thank you
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2006
    Posts
    21
    Rep Power
    0
    THe only issue with this is that if it ends on 50 minutes it's being rounded to just 5.

    So 3.5 hours
  12. #7
  13. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,274
    Rep Power
    4193
    If you're getting 3.5 out of 3 hours and 50 minutes then you're doing something wrong. The posted code works just fine for such a case:
    Previous: 2 hours, 41 minutes.
    Add 69 minutes.
    Current: 3 hours, 50 minutes.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  14. #8
  15. Banned (not really)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 1999
    Location
    Caro, Michigan
    Posts
    14,961
    Rep Power
    4575
    We kind of guessed that hours and minutes were separate variables and showed you the math.

    You'll have to show what you're actually doing or give some more info if this isn't working for you.
    -- Cigars, whiskey and wild, wild women. --

IMN logo majestic logo threadwatch logo seochat tools logo