Similar to Convert time interval given in seconds into more human readable form, but not identical:
I already have an algorithm to convert a duration given in seconds to human-readable units, like “130 weeks, 1 day, 2 hours, 54 minutes, and 27 seconds”.
However the algorithm gets messy when taking into account variable-length months and leap years.
Obviously the algorithm would have to take into account from/to where the duration is starting/ending.
Does anybody have such an algorithm?
My algorithm is written in Perl, and it is returning an array of pairs, where each pair consists of a number and a unit string.
The result is sorted by length of duration (longest first).
/a/36369554/6607497 seems to convert “both ends” of the duration to absolute calendar date, and then build the difference for each component (which would work, too, of course).
The answer in /a/36359847/6607497 shows how to compute leap years at least.
After reading In PHP, how do I get the accurate (not approximate) total individual number of weeks, months and years between two timestamps? I thing I probably don’t want to take daylight saving time leaps and leap seconds into account, but if someone wants to propose a universal solution, I won’t say no 😉