title: Slicing a time range into parts
body: <p>First question. Be gentle.</p>
  
  <p>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.") </p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>[rateTable] =&gt; Array
      (
          [Monday] =&gt; Array
              (
                  [00:00:00] =&gt; 1.5
                  [08:00:00] =&gt; 1
                  [17:00:00] =&gt; 1.5
                  [23:59:59] =&gt; 1
              )
  
          [Tuesday] =&gt; Array
              (
                  [00:00:00] =&gt; 1.5
                  [08:00:00] =&gt; 1
                  [17:00:00] =&gt; 1.5
                  [23:59:59] =&gt; 1
              )
          ...
      )
  </code></pre>
  
  <p>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. <em>(This format is entirely negotiable, but my goal is to make it as easily human-readable as possible.)</em></p>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>[output] =&gt; Array
      (
          [0] =&gt; Array
              (
                  [start] =&gt; 15:00:00
                  [stop] =&gt; 17:00:00
                  [multiplier] =&gt; 1
              )
  
          [1] =&gt; Array
              (
                  [start] =&gt; 17:00:00
                  [stop] =&gt; 21:00:00
                  [multiplier] =&gt; 1.5
              )
      )
  </code></pre>
  
  <p>I just plain can't wrap my head around the logic of splitting a single (start,stop) into (potentially) multiple subparts.</p>