Skip to content
Advertisement

Why is this addition function sometimes not working in PHP?

I’m currently writing a class to manage time in a specific format. The output is always in this format: 0000:00:0:00:00 which stands for YYYY:WW:D:HH:MM. I have a function to add time and it seemed to be working perfectly until I tried to call it several times.

I used this code to add the time, and it should have returned 0000:00:0:00:42 but it returned 0000:00:0:03:60.

Here’s my full class. Code Demo on https://3v4l.org/YiEQF

JavaScript

Any help would be much appreciated!

Advertisement

Answer

Not sure if the overall process is the best method of achieving what you want, but this is the problem ( I think ).

Just using the minute code as an example…

JavaScript

So first you add the new minutes to the existing time…

JavaScript

process the hour rollover and then you add the new minutes field again…

JavaScript

This is repeated at each layer.

So I suggest something like

JavaScript

This does similar processing, but the last part is just assigning the new minutes, rather than adding them again.

Note that I’ve also changed the way that the next level is calculated – using floor() instead of round().

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement