You are here

function date_iso_week_range in Date 5

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

Compute min and max dates for an ISO week

based on ISO weeks, which start counting on the first Monday in a week that has at least 4 days in the current year

@value - an argument in the format 2006-W20 (year + -W + week number)

Return value

an array of ISO dates representing the first and last day in the week

File

./date.inc, line 1522
Date/time API functions

Code

function date_iso_week_range($year, $week) {

  // subtract 1 from week number so we don't double-count the final week
  $weeks = intval($week - 1);

  // get a unix value for the first day of the year
  $first_day_of_year = date_iso2unix($year . '-01-01T00:00:00');

  // get to the day of week of the first day of the year, 0 is Sunday
  $dow = date_gmdate('w', $first_day_of_year);

  // ISO week counts actual first week only if it has at least 4 days in it
  if ($dow > 2) {
    $weeks += 1;
  }

  // calc adjustment from first day of year dow back or forward to Monday
  $shift = intval((1 - $dow) * 86400);

  // the day we want is $weeks away from first day of year, adjusted to the Monday of that week by $shift
  $first_day_of_week = $first_day_of_year + $weeks * 604800 + $shift;
  $last_day_of_week = $first_day_of_week + 604800 - 1;

  // convert the unix dates back to iso
  return array(
    $first_day_of_week,
    $last_day_of_week,
  );
}