diff --git a/wp-includes/functions.php b/wp-includes/functions.php index e86433b2e8..7bce735499 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -192,24 +192,15 @@ function get_weekstartend( $mysqlstring, $start_of_week = '' ) { $md = substr( $mysqlstring, 5, 2 ); // Mysql string day $day = mktime( 0, 0, 0, $md, $mm, $my ); // The timestamp for mysqlstring day. $weekday = date( 'w', $day ); // The day of the week from the timestamp - $i = 86400; // One day if ( !is_numeric($start_of_week) ) $start_of_week = get_option( 'start_of_week' ); if ( $weekday < $start_of_week ) - $weekday = 7 - $start_of_week - $weekday; + $weekday += 7; - while ( $weekday > $start_of_week ) { - $weekday = date( 'w', $day ); - if ( $weekday < $start_of_week ) - $weekday = 7 - $start_of_week - $weekday; - - $day -= 86400; - $i = 0; - } - $week['start'] = $day + 86400 - $i; - $week['end'] = $week['start'] + 604799; - return $week; + $start = $day - 86400 * ( $weekday - $start_of_week ); // The most recent week start day on or before $day + $end = $start + 604799; // $start + 7 days - 1 second + return compact( 'start', 'end' ); } /** @@ -4168,4 +4159,28 @@ function send_nosniff_header() { @header( 'X-Content-Type-Options: nosniff' ); } +/** + * Returns a MySQL expression for selecting the week number based on the start_of_week option. + * + * @internal + * @since 3.0.0 + * @param string $column + * @return string + */ +function _wp_mysql_week( $column ) { + switch ( $start_of_week = (int) get_option( 'start_of_week' ) ) { + default : + case 0 : + return "WEEK( $column, 0 )"; + case 1 : + return "WEEK( $column, 1 )"; + case 2 : + case 3 : + case 4 : + case 5 : + case 6 : + return "WEEK( DATE_SUB( $column, INTERVAL $start_of_week DAY ), 0 )"; + } +} + ?> diff --git a/wp-includes/general-template.php b/wp-includes/general-template.php index a1eabca6c1..c621918af2 100644 --- a/wp-includes/general-template.php +++ b/wp-includes/general-template.php @@ -965,8 +965,8 @@ function wp_get_archives($args = '') { } } } elseif ( 'weekly' == $type ) { - $start_of_week = get_option('start_of_week'); - $query = "SELECT DISTINCT WEEK(post_date, $start_of_week) AS `week`, YEAR(post_date) AS yr, DATE_FORMAT(post_date, '%Y-%m-%d') AS yyyymmdd, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY WEEK(post_date, $start_of_week), YEAR(post_date) ORDER BY post_date DESC $limit"; + $week = _wp_mysql_week( '`post_date`' ); + $query = "SELECT DISTINCT $week AS `week`, YEAR( `post_date` ) AS `yr`, DATE_FORMAT( `post_date`, '%Y-%m-%d' ) AS `yyyymmdd`, count( `ID` ) AS `posts` FROM `$wpdb->posts` $join $where GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` DESC $limit"; $key = md5($query); $cache = wp_cache_get( 'wp_get_archives' , 'general'); if ( !isset( $cache[ $key ] ) ) { diff --git a/wp-includes/query.php b/wp-includes/query.php index 7467fbfe9d..2a16154924 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -1780,7 +1780,7 @@ class WP_Query { } if ( $q['w'] ) - $where .= " AND WEEK($wpdb->posts.post_date, 1)='" . $q['w'] . "'"; + $where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'"; if ( intval($q['comments_popup']) ) $q['p'] = absint($q['comments_popup']);