You are here

function calendar_week_year in Calendar 5

Find the calendar week number and year for a date.

This is complicated by the fact that the calendar week does not match the ISO week, since the ISO week starts on Monday for the first week that has 4 or more days in the new year while the calendar week starts on the site's preferred first day of the week regardless of the number of days that are in the first week or the year.

Parameters

unknown_type $timestamp:

Return value

array of calendar week number and year

2 calls to calendar_week_year()
calendar_get_paths in ./calendar.module
calendar_week in ./calendar.module
Handle a lot of messy week calculations all in one place to make maintenance easier

File

./calendar.module, line 1001
Adds calendar filtering and displays to Views.

Code

function calendar_week_year($timestamp) {
  $first_day = variable_get('date_first_day', 0);
  $iso_week = intval(date_format_date('W', $timestamp));
  $year = date_format_date('Y', $timestamp);

  // Where does this timestamp fall within the range for the iso week number.
  $range = calendar_week_range($year, $iso_week);

  // If the timestamp is in the range, the ISO week number is correct.
  if ($timestamp >= $range[0] && $timestamp <= $range[1]) {
    return array(
      $iso_week,
      $year,
    );
  }
  elseif ($timestamp < $range[1]) {
    if ($iso_week >= 2) {
      return array(
        intval($iso_week - 1),
        $year,
      );
    }
    else {
      return array(
        1,
        $year,
      );
    }
  }
  elseif ($timestamp > $range[0]) {
    if ($iso_week < 52) {
      return array(
        intval($iso_week + 1),
        $year,
      );
    }
    else {
      return array(
        1,
        intval($year + 1),
      );
    }
  }
}