You are here

function date_repeat_add_dates in Date 7.3

Same name and namespace in other branches
  1. 5.2 date_repeat/date_repeat_calc.inc \date_repeat_add_dates()
  2. 6.2 date_repeat/date_repeat_calc.inc \date_repeat_add_dates()
  3. 6 date_repeat/date_repeat_calc.inc \date_repeat_add_dates()
  4. 7 date_repeat/date_repeat_calc.inc \date_repeat_add_dates()
  5. 7.2 date_repeat/date_repeat_calc.inc \date_repeat_add_dates()

Helper function to add found date to the $dates array.

Check that the date to be added is between the start and end date and that it is not in the $exceptions, nor already in the $days array, and that it meets other criteria in the RRULE.

1 call to date_repeat_add_dates()
_date_repeat_calc in date_repeat/date_repeat_calc.inc
Private implementation of date_repeat_calc().

File

date_repeat/date_repeat_calc.inc, line 444
Code to compute the dates that match an iCal RRULE.

Code

function date_repeat_add_dates(&$days, $current_day, $start_date, $end_date, $exceptions, $rrule) {
  if (isset($rrule['COUNT']) && count($days) >= $rrule['COUNT']) {
    return FALSE;
  }
  $formatted = date_format($current_day, DATE_FORMAT_DATETIME);
  if (!empty($end_date) && $formatted > date_format($end_date, DATE_FORMAT_DATETIME)) {
    return FALSE;
  }
  if ($formatted < date_format($start_date, DATE_FORMAT_DATETIME)) {
    return FALSE;
  }
  if (in_array(date_format($current_day, 'Y-m-d'), $exceptions)) {
    return FALSE;
  }
  if (!empty($rrule['BYDAY'])) {
    $by_days = $rrule['BYDAY'];
    foreach ($by_days as $delta => $by_day) {
      $by_days[$delta] = substr($by_day, -2);
    }
    if (!in_array(date_repeat_dow2day(date_format($current_day, 'w')), $by_days)) {
      return FALSE;
    }
  }
  if (!empty($rrule['BYYEAR']) && !in_array(date_format($current_day, 'Y'), $rrule['BYYEAR'])) {
    return FALSE;
  }
  if (!empty($rrule['BYMONTH']) && !in_array(date_format($current_day, 'n'), $rrule['BYMONTH'])) {
    return FALSE;
  }
  if (!empty($rrule['BYMONTHDAY'])) {

    // Test month days, but only if there are no negative numbers.
    $test = TRUE;
    $by_month_days = array();
    foreach ($rrule['BYMONTHDAY'] as $day) {
      if ($day > 0) {
        $by_month_days[] = $day;
      }
      else {
        $test = FALSE;
        break;
      }
    }
    if ($test && !empty($by_month_days) && !in_array(date_format($current_day, 'j'), $by_month_days)) {
      return FALSE;
    }
  }

  // Don't add a day if it is already saved so we don't throw the count off.
  if (in_array($formatted, $days)) {
    return TRUE;
  }
  else {
    $days[] = $formatted;
  }
}