You are here

function date_iso_week_range in Date 7.2

Same name and namespace in other branches
  1. 8 date_api/date_api.module \date_iso_week_range()
  2. 5 date.inc \date_iso_week_range()
  3. 6.2 date_api.module \date_iso_week_range()
  4. 7.3 date_api/date_api.module \date_iso_week_range()
  5. 7 date_api/date_api.module \date_iso_week_range()

Calculates the start and end dates for an ISO week.

Parameters

int $week: The week value.

int $year: The year value.

Return value

array A numeric array containing the start and end dates of an ISO week.

1 call to date_iso_week_range()
date_week_range in date_api/date_api.module
Calculates the start and end dates for a calendar week.

File

date_api/date_api.module, line 2158
This module will make the date API available to other modules.

Code

function date_iso_week_range($week, $year) {

  // Get to the last ISO week of the previous year.
  $min_date = new DateObject($year - 1 . '-12-28 00:00:00');
  date_timezone_set($min_date, date_default_timezone_object());

  // Find the first day of the first ISO week in the year.
  // If it's already a Monday, date_modify won't add a Monday,
  // it will remain the same day. So add a Sunday first, then a Monday.
  date_modify($min_date, '+1 Sunday');
  date_modify($min_date, '+1 Monday');

  // Jump ahead to the desired week for the beginning of the week range.
  if ($week > 1) {
    date_modify($min_date, '+ ' . ($week - 1) . ' weeks');
  }

  // Move forwards to the last day of the week.
  $max_date = clone $min_date;
  date_modify($max_date, '+6 days +23 hours +59 minutes +59 seconds');
  return array(
    $min_date,
    $max_date,
  );
}