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

Join Date
Apr 2006
Posts
21
Rep Power
0

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. 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
3. 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?
4. 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.

• Sepodati agrees
5. 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
6. 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
7. 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.