WordPress.org

Make WordPress Core

Ticket #11799: 11799-bonus-mysql2date.patch

File 11799-bonus-mysql2date.patch, 2.5 KB (added by hakre, 8 years ago)

Bonus refactoring - Orphaned code removed, Docblock update, propper returns in mysql2date()

  • wp-includes/functions.php

    ### Eclipse Workspace Patch 1.0
    #P wordpress-trunk
     
    66 */
    77
    88/**
    9  * Converts MySQL DATETIME field to user specified date format.
     9 * Format MySQL Datetime
     10 *
     11 * Converts MySQL DATETIME field to specified format.
     12 *
     13 * Format:
    1014 *
    11  * If $dateformatstring has 'G' value, then gmmktime() function will be used to
    12  * make the time. If $dateformatstring is set to 'U', then mktime() function
    13  * will be used to make the time.
    14  *
    15  * The $translate will only be used, if it is set to true and it is by default
    16  * and if the $wp_locale object has the month and weekday set.
    17  *
     15 *   'G'   : strtotime ( date +0000 )
     16 *   'U'   : strtotime ( date )
     17 *   other : format according to php date() function
     18 *
     19 * @link http://www.php.net/strtotime
     20 * @link http://www.php.net/date
     21 *
     22 * Prevent locale formattings by setting the optional $translate parameter
     23 * to false.
     24 *
    1825 * @since 0.71
    19  *
    20  * @param string $dateformatstring Either 'G', 'U', or php date format.
    21  * @param string $mysqlstring Time from mysql DATETIME field.
    22  * @param bool $translate Optional. Default is true. Will switch format to locale.
    23  * @return string Date formated by $dateformatstring or locale (if available).
     26 * @param string  $dateformatstring Either 'G', 'U', or php date format.
     27 * @param string  $mysqlstring      Time from mysql DATETIME field.
     28 * @param bool    $translate        (optional) Format to locale, default is true.
     29 * @return string|false Formatted date, false on error.
    2430 */
    25 function mysql2date( $dateformatstring, $mysqlstring, $translate = true ) {
    26         global $wp_locale;
    27         $m = $mysqlstring;
    28         if ( empty( $m ) )
     31function mysql2date( $dateformatstring, $mysqlstring, $translate = true ) {     
     32        if ( empty( $mysqlstring ) )
    2933                return false;
    3034
    31         if( 'G' == $dateformatstring ) {
    32                 return strtotime( $m . ' +0000' );
    33         }
     35        if( 'G' == $dateformatstring )
     36                return strtotime( $mysqlstring . ' +0000' );
    3437
    35         $i = strtotime( $m );
     38        $timestamp = strtotime( $mysqlstring );
     39       
     40        if ( false === $timestamp )
     41                return false;
    3642
    3743        if( 'U' == $dateformatstring )
    38                 return $i;
     44                return $time;
    3945
    40         if ( $translate)
    41             return date_i18n( $dateformatstring, $i );
     46        if ( $translate )
     47            return date_i18n( $dateformatstring, $timestamp );
    4248        else
    43             return date( $dateformatstring, $i );
     49            return date( $dateformatstring, $timestamp );
    4450}
    4551
    4652/**