public static function buildFbString($fb, Horde_Date $start, Horde_Date $end)
{
if (empty($fb)) {
return false;
}
// Calculate total time span.
$end_ts = $end->timestamp();
$start_ts = $start->timestamp();
$sec = $end_ts - $start_ts;
$fb_start = new Horde_Date($fb->s);
$fb_end = new Horde_Date($fb->e);
// Number of 30 minute periods.
$period_cnt = ceil($sec / 1800);
// Requested range is completely out of the available range.
if ($start_ts >= $fb_end->timestamp() || $end_ts < $fb_start->timestamp()) {
return str_repeat('4', $period_cnt);
}
// We already know we don't have any busy periods.
if (empty($fb->b) && $fb_end->timestamp() <= $end_ts) {
return str_repeat('0', $period_cnt);
}
$eas_fb = '';
// Move $start to the start of the available data.
while ($start_ts < $fb_start->timestamp() && $start_ts <= $end_ts) {
$eas_fb .= '4';
$start_ts += 1800;
// 30 minutes
}
// The rest is assumed free up to $fb->e
while ($start_ts <= $fb_end->timestamp() && $start_ts <= $end_ts) {
$eas_fb .= '0';
$start_ts += 1800;
}
// The remainder is also unavailable
while ($start_ts <= $end_ts) {
$eas_fb .= '4';
$start_ts += 1800;
}
// Now put in the busy blocks.
while (list($b_start, $b_end) = each($fb->b)) {
$offset = $b_start - $start->timestamp();
$duration = ceil(($b_end - $b_start) / 1800);
if ($offset > 0) {
$eas_fb = substr_replace($eas_fb, str_repeat('2', $duration), floor($offset / 1800), $duration);
}
}
return $eas_fb;
}