Time Adder
So one thing I end up doing a lot for different reasons is adding up times in my head, for example trying to do something like 26m50s + 17m30s = 44:20, and it always ends up more difficult than I expect. I'm pretty good at mental maths but the base 60 nature of it just throws me, especially if I'm trying to add a few at once.
So I made something to help, Time Adder, just enter in as many times as you want, comma separated and it'll add them up and print a simple result.
This was a pretty fun way to spend 20 minutes of a morning. It's not an especially complex algorithm and I have a nagging feeling there's some better way to do things I've missed, but here's a quick breakdown:
- Get all the times entered and explode them on the comma so you've got an array
- Declare 3 variables, $hours, $minutes, $seconds all with a value of 0
- For each time, split it on the : and then add the values to the relevant variable
- While $seconds >= 60, decrease it by 60 and increase $minutes by 1
- While $minutes >= 60, decrease it by 60 and increase $hours by 1
- Implode all three variables and return that string;