给定一天中的事件列表,我将如何计算这些事件之间的差距?

In my application I have an array of events coming out from a database, with each event having a start_time and a finish_time. In the current version of the application, I draw these out on a canvas with each event being a <div> with certain styling.

However, I now need to do the same thing, but instead only display the times when the user is free (ie. when there are no events). What is the best way to end up with an array that has blocks of free time with a start_time and finish_time as opposed to the events?

Assuming $events sorted ASC by start_time and there is no invalid events with start_time > end_time also events do not intersect in time.

$events = array(
array('start_time', 'finish_time'),
array('start_time', 'finish_time'),
...
);

foreach($events as $key => $event) {
  if(isset($events[$key+1]))
   $free_time[] = array(
    'start_time' => $event['finish_time'],
    'end_time' => $events[$key+1]['start_time']
  );
 }
}

Same basic principle as in Zuker's response but instead of relying on a continous array using a "previous state" variable.

<?php
$result = array();

// *
$end = '00:00:00';     
foreach( getData() as $d ) {
    if ( $d['start_time']!=$end ) {
        $result[] = array('name'=>'available', 'start_time'=>$end, 'finish_time'=>$d['start_time'] );
    }
    $end = $d['finish_time'];       
    $result[] = $d;
}
// *
$result[] = array('name'=>'available', 'start_time'=>$end, 'finish_time'=>'23:59:59' );

print_r($result);



function getData() {
    return array(
        array(
            'name'=>'event A',
            'start_time'=>'01:45:00',
            'finish_time'=>'02:30:15'
        ),
        array(
            'name'=>'event B',
            'start_time'=>'02:30:15',
            'finish_time'=>'03:00:00'
        ),
        array(
            'name'=>'event C',
            'start_time'=>'03:15:00',
            'finish_time'=>'06:15:00'
        ),
    );
}

prints

Array
(
    [0] => Array
        (
            [name] => available
            [start_time] => 00:00:00
            [finish_time] => 01:45:00
        )

    [1] => Array
        (
            [name] => event A
            [start_time] => 01:45:00
            [finish_time] => 02:30:15
        )

    [2] => Array
        (
            [name] => event B
            [start_time] => 02:30:15
            [finish_time] => 03:00:00
        )

    [3] => Array
        (
            [name] => available
            [start_time] => 03:00:00
            [finish_time] => 03:15:00
        )

    [4] => Array
        (
            [name] => event C
            [start_time] => 03:15:00
            [finish_time] => 06:15:00
        )

    [5] => Array
        (
            [name] => available
            [start_time] => 06:15:00
            [finish_time] => 23:59:59
        )

)

(*) this does not cover events that begin on the previous day and end on the current day or begin on the current day and end on the next day. For brevity's sake I left out the date part, but those "edge cases" should be trivial.