WordPress.org

Make WordPress Core

Changeset 14499


Ignore:
Timestamp:
05/07/10 05:01:29 (4 years ago)
Author:
nacin
Message:

Fix start of week and SQL WEEK handling. props mdawaffe, fixes #10397.

Location:
trunk/wp-includes
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r14495 r14499  
    193193    $day = mktime( 0, 0, 0, $md, $mm, $my ); // The timestamp for mysqlstring day. 
    194194    $weekday = date( 'w', $day ); // The day of the week from the timestamp 
    195     $i = 86400; // One day 
    196195    if ( !is_numeric($start_of_week) ) 
    197196        $start_of_week = get_option( 'start_of_week' ); 
    198197 
    199198    if ( $weekday < $start_of_week ) 
    200         $weekday = 7 - $start_of_week - $weekday; 
    201  
    202     while ( $weekday > $start_of_week ) { 
    203         $weekday = date( 'w', $day ); 
    204         if ( $weekday < $start_of_week ) 
    205             $weekday = 7 - $start_of_week - $weekday; 
    206  
    207         $day -= 86400; 
    208         $i = 0; 
    209     } 
    210     $week['start'] = $day + 86400 - $i; 
    211     $week['end'] = $week['start'] + 604799; 
    212     return $week; 
     199        $weekday += 7; 
     200 
     201    $start = $day - 86400 * ( $weekday - $start_of_week ); // The most recent week start day on or before $day 
     202    $end = $start + 604799; // $start + 7 days - 1 second 
     203    return compact( 'start', 'end' ); 
    213204} 
    214205 
     
    41694160} 
    41704161 
     4162/** 
     4163 * Returns a MySQL expression for selecting the week number based on the start_of_week option. 
     4164 * 
     4165 * @internal 
     4166 * @since 3.0.0 
     4167 * @param string $column 
     4168 * @return string 
     4169 */ 
     4170function _wp_mysql_week( $column ) { 
     4171    switch ( $start_of_week = (int) get_option( 'start_of_week' ) ) { 
     4172    default : 
     4173    case 0 : 
     4174        return "WEEK( $column, 0 )"; 
     4175    case 1 : 
     4176        return "WEEK( $column, 1 )"; 
     4177    case 2 : 
     4178    case 3 : 
     4179    case 4 : 
     4180    case 5 : 
     4181    case 6 : 
     4182        return "WEEK( DATE_SUB( $column, INTERVAL $start_of_week DAY ), 0 )"; 
     4183    } 
     4184} 
     4185 
    41714186?> 
  • trunk/wp-includes/general-template.php

    r14486 r14499  
    966966        } 
    967967    } elseif ( 'weekly' == $type ) { 
    968         $start_of_week = get_option('start_of_week'); 
    969         $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"; 
     968        $week = _wp_mysql_week( '`post_date`' ); 
     969        $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"; 
    970970        $key = md5($query); 
    971971        $cache = wp_cache_get( 'wp_get_archives' , 'general'); 
  • trunk/wp-includes/query.php

    r14478 r14499  
    17811781 
    17821782        if ( $q['w'] ) 
    1783             $where .= " AND WEEK($wpdb->posts.post_date, 1)='" . $q['w'] . "'"; 
     1783            $where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'"; 
    17841784 
    17851785        if ( intval($q['comments_popup']) ) 
Note: See TracChangeset for help on using the changeset viewer.