1 date.inc date_week_range($week, $year)

Calculates the start and end dates for a calendar week.

The dates are adjusted to use the chosen first day of week for this site.

Parameters

int $week: The week value.

int $year: The year value.

Return value

array: A numeric array containing the start and end dates of a week.

File

core/includes/date.inc, line 937
Date API functions.

Code

function date_week_range($week, $year) {
  $first_day = config_get('system.date', 'first_day');

  // If the first day of the week is Monday, use the ISO-8601 defined range.
  // See https://en.wikipedia.org/wiki/Week#Week_numbering
  if ($first_day == 1) {
    return date_iso_week_range($week, $year);
  }

  $min_date = new BackdropDateTime($year . '-01-01 00:00:00');
  $min_date->setTimezone(date_default_timezone_object());

  // Move to the right week.
  date_modify($min_date, '+' . strval(7 * ($week - 1)) . ' days');

  // Move backwards to the first day of the week.
  $day_wday = date_format($min_date, 'w');
  date_modify($min_date, '-' . strval((7 + $day_wday - $first_day) % 7) . ' days');

  // Move forwards to the last day of the week.
  $max_date = clone($min_date);
  date_modify($max_date, '+6 days');

  if (date_format($min_date, 'Y') != $year) {
    $min_date = new BackdropDateTime($year . '-01-01 00:00:00');
  }
  return array($min_date, $max_date);
}