function constrain($events, $gmt_start = null, $gmt_end = null, $limit = null) { $repeats = ICalEvents::collapse_repeats($events, $gmt_start, $gmt_end, $limit); if (is_array($repeats) and count($repeats) > 0) { $events = array_merge($events, $repeats); } $events = ICalEvents::sort_by_key($events, 'StartTime'); if (!$limit) { $limit = count($events); } $constrained = array(); $count = 0; foreach ($events as $event) { if (ICalEvents::falls_between($event, $gmt_start, $gmt_end)) { $constrained[] = $event; ++$count; } if ($count >= $limit) { break; } } return $constrained; }