You are here

function date_week in Date 6

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

The calendar week number for a date.

PHP week functions return the ISO week, not the calendar week.

Parameters

string $date, in the format Y-m-d:

Return value

int calendar week number.

2 calls to date_week()
date_weeks_in_year in ./date_api.module
The number of calendar weeks in a year.
template_preprocess_date_navigation in theme/theme.inc
Preprocessor to construct back and next navigation from the date argument.

File

./date_api.module, line 850
This module will make the date API available to other modules. Designed to provide a light but flexible assortment of functions and constants, with more functionality in additional files that are not loaded unless other modules specifically include them.

Code

function date_week($date) {
  $parts = explode('-', $date);
  $date = date_make_date($date . ' 12:00:00', 'UTC');
  $year_date = date_make_date($parts[0] . '-01-01 12:00:00', 'UTC');
  $week = intval(date_format($date, 'W'));
  $year_week = intval(date_format($year_date, 'W'));
  $date_year = intval(date_format($date, 'o'));

  // remove the leap week if it's present
  if ($date_year > intval($parts[0])) {
    $last_date = drupal_clone($date);
    date_modify($last_date, '-7 days');
    $week = date_format($last_date, 'W') + 1;
  }
  else {
    if ($date_year < intval($parts[0])) {
      $week = 0;
    }
  }
  if ($year_week != 1) {
    $week++;
  }

  // convert to ISO-8601 day number, to match weeks calculated above
  $iso_first_day = 1 + (variable_get('date_first_day', 0) + 6) % 7;

  // if it's before the starting day, it's the previous week
  if (intval(date_format($date, 'N')) < $iso_first_day) {
    $week--;
  }

  // if the year starts before, it's an extra week at the beginning
  if (intval(date_format($year_date, 'N')) < $iso_first_day) {
    $week++;
  }
  return $week;
}