Slicing a time range into parts
- by beporter
First question. Be gentle.
I'm working on software that tracks technicians' time spent working on tasks. The software needs to be enhanced to recognize different billable rate multipliers based on the day of the week and the time of day. (For example, "Time and a half after 5 PM on weekdays.")
The tech using the software is only required to log the date, his start time and his stop time (in hours and minutes). The software is expected to break the time entry into parts at the boundaries of when the rate multipliers change. A single time entry is not permitted to span multiple days.
Here is a partial sample of the rate table. The first-level array keys are the days of the week, obviously. The second-level array keys represent the time of the day when the new multiplier kicks in, and runs until the next sequential entry in the array. The array values are the multiplier for that time range.
[rateTable] => Array
(
[Monday] => Array
(
[00:00:00] => 1.5
[08:00:00] => 1
[17:00:00] => 1.5
[23:59:59] => 1
)
[Tuesday] => Array
(
[00:00:00] => 1.5
[08:00:00] => 1
[17:00:00] => 1.5
[23:59:59] => 1
)
...
)
In plain English, this represents a time-and-a-half rate from midnight to 8 am, regular rate from 8 to 5 pm, and time-and-a-half again from 5 till 11:59 pm. The time that these breaks occur may be arbitrary to the second and there can be an arbitrary number of them for each day. (This format is entirely negotiable, but my goal is to make it as easily human-readable as possible.)
As an example: a time entry logged on Monday from 15:00:00 (3 PM) to 21:00:00 (9 PM) would consist of 2 hours billed at 1x and 4 hours billed at 1.5x. It is also possible for a single time entry to span multiple breaks. Using the example rateTable above, a time entry from 6 AM to 9 PM would have 3 sub-ranges from 6-8 AM @ 1.5x, 8AM-5PM @ 1x, and 5-9 PM @ 1.5x. By contrast, it's also possible that a time entry may only be from 08:15:00 to 08:30:00 and be entirely encompassed in the range of a single multiplier.
I could really use some help coding up some PHP (or at least devising an algorithm) that can take a day of the week, a start time and a stop time and parse into into the required subparts. It would be ideal to have the output be an array that consists of multiple entries for a (start,stop,multiplier) triplet. For the above example, the output would be:
[output] => Array
(
[0] => Array
(
[start] => 15:00:00
[stop] => 17:00:00
[multiplier] => 1
)
[1] => Array
(
[start] => 17:00:00
[stop] => 21:00:00
[multiplier] => 1.5
)
)
I just plain can't wrap my head around the logic of splitting a single (start,stop) into (potentially) multiple subparts.