You are here

function node_recur_generate_dates_rule in Node recur 7

Generate an array of recurring dates based on the provided rule criteria

Parameters

$node: The node that's being recurred

$date: The initial starting date belonging to the node that will be recurring. Can be in string or numeric format.

$frequency: The frequency that the period occurs, ie, every 5 days, the frequency would be 5.

$period: The period of each frequency, ie, every 5 days, the period will be 'day'. It can also be 'week' or 'month'.

$until: The date to recur until. Can be in string or numeric format.

$weekends: TRUE if weekends should be included.

Return value

An array of timestamps

1 call to node_recur_generate_dates_rule()
node_recur_generate_dates_from_form in ./node_recur.module
Generate dates from a form state

File

./node_recur.module, line 307

Code

function node_recur_generate_dates_rule($node, $date, $frequency, $period, $until, $weekends = TRUE) {
  $dates = array();
  $month = FALSE;

  // Convert date and until date to timestamp, if needed
  $date = is_string($date) ? strtotime($date) : $date;
  $until = is_string($until) ? strtotime($until) : $until;

  // Make sure we have valid timestamps
  if (!is_numeric($date) || !is_numeric($until)) {
    return FALSE;
  }

  // Make sure the until is ahead of the date
  if ($date >= $until) {
    return FALSE;
  }

  // Convert month period to weeks, in order to preserve the day
  // of the week
  if ($period == 'month') {
    $frequency = $frequency * 4;
    $period = 'week';
    $month = TRUE;
  }

  // Track the current date
  $current = $date;

  // Iterate and generate dates until we reach the end
  while (TRUE) {

    // Generate the next date
    $next = strtotime("+{$frequency} " . format_plural($frequency, $period, "{$period}s"), $current);

    // If this is a month recur, we need to make sure the the next date
    // is on the next month. Some months have 5 repeats of the same day
    if ($month && date('n', $next) == date('n', $current)) {

      // Jump forward one more week
      $next = strtotime('+1 week', $next);
    }
    $current = $next;

    // Make sure date is in the future, if the settings dictate that
    if (!node_recur_allow_past_dates($node->type) && $next < REQUEST_TIME) {
      continue;
    }

    // If we're excluding weekends, skip this if it's a weekend
    if (!$weekends) {
      $day = date('D', $next);
      if ($day == 'Sun' || $day == 'Sat') {
        continue;
      }
    }

    // See if this date puts us past the limit
    if ($next > $until) {
      break;
    }
    $dates[] = $current;
  }
  return $dates;
}