You are here

private static function MerciHours::weekDaysDates in MERCI (Manage Equipment Reservations, Checkout and Inventory) 7.3

Returns dates in a period corresponding to requested week days.

Internally it builds a rrule to do the job.

2 calls to MerciHours::weekDaysDates()
MerciHours::getClosedDaysInPeriod in merci_hours/includes/entity.inc
Returns a list of closed days for the requested period.
MerciHours::getOpenDaysInPeriod in merci_hours/includes/entity.inc
Returns a list of opening days for the requested period.

File

merci_hours/includes/entity.inc, line 115

Class

MerciHours
Define a WorkCalendar object with its operations.

Code

private static function weekDaysDates($start, $end, $week_days) {

  // Transform $week_days to a rrule "byday" string. Example: SU,MO,TU,WE.
  // date_repeat api wants the byday array to start by the week day of $start.
  $byday = array();

  // Convert $week_days to a key based array and find the index of the week
  // day of start in order to assign bydays starting from this index.
  $week_days = array_values($week_days);
  $index = array_search(self::weekDayName($start), $week_days);
  $count = count($week_days);
  for ($offset = 0; $offset < $count; $offset++) {
    $wday = $week_days[($index + $offset) % $count];
    $byday[] = strtoupper(substr($wday, 0, 2));
  }

  // Request date_repeat to run the rrule.
  $rrule = 'RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=' . implode(',', $byday) . ';';

  // Don't require date_repeat module to be enabled. Just use its api.
  module_load_include('module', 'date_repeat', 'date_repeat');
  module_load_include('inc', 'date_repeat', 'date_repeat_calc');
  $dates = date_repeat_calc($rrule, (string) $start, (string) $end);

  // Strip hours from dates.
  foreach ($dates as $key => $date) {
    $dates[$key] = substr($date, 0, 10);
  }

  // Special case. First day is always included in despite of the rrule.
  // We need to exclude it by hand if neccesary.
  $week_day = self::weekDayName($dates[0]);
  if (!in_array($week_day, $week_days)) {
    array_shift($dates);
  }
  return $dates;
}