diff --git a/date_api/date_api.module b/date_api/date_api.module index 160bc9fb..400c6339 100644 --- a/date_api/date_api.module +++ b/date_api/date_api.module @@ -2177,6 +2177,40 @@ function date_week($date) { } /** + * The year of the calendar week number for a date. + * + * PHP week functions return the ISO week, not the calendar week, + * which, for week 1, could be a week 52/53 of the previous year. + * + * @param string $date + * A date string in the format Y-m-d. + * + * @return int + * The year for the calendar week. + */ +function date_week_year($date) { + $date = substr($date, 0, 10); + $parts = explode('-', $date); + + $date = new DateObject($date . ' 12:00:00', 'UTC'); + + // If we are using ISO weeks, this is easy. + if (variable_get('date_api_use_iso8601', FALSE)) { + return intval($date->format('o')); + } + + $year_date = new DateObject($parts[0] . '-01-01 12:00:00', 'UTC'); + $date_year = intval($date->format('o')); + + // If the ISO year is previous year, use given year. + if ($date_year < intval($parts[0])) { + return int_val($parts[0]); + } + + return intval($date_year); +} + +/** * Helper function to left pad date parts with zeros. * * Provided because this is needed so often with dates. diff --git a/date_views/date_views.module b/date_views/date_views.module index c050e56e..a8a4a848 100644 --- a/date_views/date_views.module +++ b/date_views/date_views.module @@ -284,7 +284,7 @@ function date_pager_url($view, $date_type = NULL, $date_arg = NULL, $force_view_ break; case 'week': - $args[$pos] = date_pad($view->date_info->year, 4) . '-W' . date_pad($view->date_info->week); + $args[$pos] = date_pad($view->date_info->week_year, 4) . '-W' . date_pad($view->date_info->week); break; case 'day': diff --git a/date_views/includes/date_views_argument_handler_simple.inc b/date_views/includes/date_views_argument_handler_simple.inc index 2839b959..198d9096 100644 --- a/date_views/includes/date_views_argument_handler_simple.inc +++ b/date_views/includes/date_views_argument_handler_simple.inc @@ -72,7 +72,7 @@ class date_views_argument_handler_simple extends views_handler_argument_date { if ($granularity == 'week') { $now = date_now(); $week = date_week(date_format($now, 'Y-m-d')); - $value = date_format($now, 'o') . '-W' . date_pad($week); + $value = date_week_year(date_format($now, 'Y-m-d')) . '-W' . date_pad($week); } else { $value = date($this->arg_format, REQUEST_TIME); diff --git a/date_views/includes/date_views_plugin_pager.inc b/date_views/includes/date_views_plugin_pager.inc index 1addd200..297d4deb 100644 --- a/date_views/includes/date_views_plugin_pager.inc +++ b/date_views/includes/date_views_plugin_pager.inc @@ -286,9 +286,10 @@ class date_views_plugin_pager extends views_plugin_pager { } // Write the date_info properties that depend on the current value. $this->view->date_info->year = date_format($argument->min_date, 'Y'); - $this->view->date_info->month = date_format($argument->min_date, 'n');; + $this->view->date_info->month = date_format($argument->min_date, 'n'); $this->view->date_info->day = date_format($argument->min_date, 'j'); $this->view->date_info->week = date_week(date_format($argument->min_date, DATE_FORMAT_DATE)); + $this->view->date_info->week_year = date_week_year(date_format($argument->min_date, DATE_FORMAT_DATE)); $this->view->date_info->date_range = $argument->date_range; $this->view->date_info->min_date = $argument->min_date; $this->view->date_info->max_date = $argument->max_date; diff --git a/date_views/theme/theme.inc b/date_views/theme/theme.inc index 7ca03f3a..e860d393 100644 --- a/date_views/theme/theme.inc +++ b/date_views/theme/theme.inc @@ -77,7 +77,7 @@ function template_preprocess_date_views_pager(&$vars) { switch ($granularity) { case 'week': $prev_week = date_week(date_format($prev_date, 'Y-m-d')); - $prev_arg = date_format($prev_date, 'o-\W') . date_pad($prev_week); + $prev_arg = date_week_year(date_format($prev_date, 'Y-m-d')) . '-W' . date_pad($prev_week); break; default: $prev_arg = date_format($prev_date, $format[$granularity]); @@ -90,7 +90,7 @@ function template_preprocess_date_views_pager(&$vars) { switch ($granularity) { case 'week': $next_week = date_week(date_format($next_date, 'Y-m-d')); - $next_arg = date_format($next_date, 'o-\W') . date_pad($next_week); + $next_arg = date_week_year(date_format($next_date, 'Y-m-d')) . '-W' . date_pad($next_week); break; default: $next_arg = date_format($next_date, $format[$granularity]); @@ -210,7 +210,7 @@ function theme_date_nav_title($params) { $title = t('Week of @date', array( '@date' => date_format_date($date_info->min_date, 'custom', $format), )); - $date_arg = $date_info->year . '-W' . date_pad($date_info->week); + $date_arg = $date_info->week_year . '-W' . date_pad($date_info->week); break; } if (!empty($date_info->mini) || $link) {