WordPress.org

Make WordPress Core

Ticket #6913: time_format_6913.diff

File time_format_6913.diff, 7.1 KB (added by regulatethis, 10 years ago)

Update of patch, fixing so 24 hour clock displays as default for an existing user without the option set (Thanks Alexander Beutl for pointing this out)

  • wp-admin/includes/post.php

     
    8181                }
    8282        }
    8383
     84        // Let's handle the conversion to 24 hour time if the user has chosen 12 hour for posting
     85        if( !empty( $_POST['time_format'] ) && $_POST['time_format'] == '12_hour' ) {
     86                // If only the meridiem has been switched, we still need to update the time...
     87                if( !empty( $_POST['hidden_meridiem'] ) && $_POST['hidden_meridiem'] != $_POST['meridiem'] )
     88                        $_POST['edit_date'] = '1';
     89               
     90                // "Convert" hour back to 24-hour format if necessary, taking the meridiem into account...
     91                if ( !empty( $_POST['time_format'] ) && $_POST['time_format'] == '12_hour' ) {
     92                       
     93                        // If the user accidentally enters a 24-hour time then we implicitly fix it for him
     94                        $_POST['hh'] = $_POST['hh'] <= 12 ? $_POST['hh'] : $_POST['hh'] - 12;
     95                       
     96                        if ( $_POST['meridiem'] == 'pm' ) {
     97                                $_POST['hh'] += 12;
     98                        }
     99                }
     100        }
     101               
    84102        if (!empty ( $_POST['edit_date'] ) ) {
    85103                $aa = $_POST['aa'];
    86104                $mm = $_POST['mm'];
  • wp-admin/includes/template.php

     
    844844
    845845        $time_adj = time() + (get_option( 'gmt_offset' ) * 3600 );
    846846        $post_date = ($for_post) ? $post->post_date : $comment->comment_date;
     847        $time_format = get_user_option('time_format');
     848        $hour_format = $time_format == '12_hour' ? 'g' : 'H';
    847849        $jj = ($edit) ? mysql2date( 'd', $post_date ) : gmdate( 'd', $time_adj );
    848850        $mm = ($edit) ? mysql2date( 'm', $post_date ) : gmdate( 'm', $time_adj );
    849851        $aa = ($edit) ? mysql2date( 'Y', $post_date ) : gmdate( 'Y', $time_adj );
    850         $hh = ($edit) ? mysql2date( 'H', $post_date ) : gmdate( 'H', $time_adj );
     852        $hh = ($edit) ? mysql2date( $hour_format, $post_date ) : gmdate( $hour_format, $time_adj );
    851853        $mn = ($edit) ? mysql2date( 'i', $post_date ) : gmdate( 'i', $time_adj );
    852854        $ss = ($edit) ? mysql2date( 's', $post_date ) : gmdate( 's', $time_adj );
    853 
     855       
     856        if( '12_hour' == $time_format ) {
     857                $meridiem = ($edit) ? date( 'a', strtotime($post_date) ) : date( 'a' );
     858                $locale_meridiem = ($edit) ? $wp_locale->get_meridiem( date( 'A', strtotime($post_date) ) ) : $wp_locale->get_meridiem( date( 'A' ) );
     859                $locale_am = $wp_locale->get_meridiem( 'AM' );
     860                $locale_pm = $wp_locale->get_meridiem( 'PM' );
     861        }
    854862        $month = "<select id=\"mm\" name=\"mm\"$tab_index_attribute>\n";
    855863        for ( $i = 1; $i < 13; $i = $i +1 ) {
    856864                $month .= "\t\t\t" . '<option value="' . zeroise($i, 2) . '"';
     
    864872        $year = '<input type="text" id="aa" name="aa" value="' . $aa . '" size="4" maxlength="5"' . $tab_index_attribute . ' autocomplete="off"  />';
    865873        $hour = '<input type="text" id="hh" name="hh" value="' . $hh . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off"  />';
    866874        $minute = '<input type="text" id="mn" name="mn" value="' . $mn . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off"  />';
     875       
     876        if( '12_hour' == $time_format ) {
     877                $am_selected = $meridiem == 'am' ? 'selected="selected"' : '';
     878                $pm_selected = $meridiem == 'pm' ? 'selected="selected"' : '';
     879               
     880                $meridiem_drop_down = '<select name="meridiem" id="meridiem"' . $tab_index_attribute . '>' . PHP_EOL;
     881                $meridiem_drop_down .= '        <option value="am" ' . $am_selected . '>' . $locale_am . '</option>' . PHP_EOL;
     882                $meridiem_drop_down .= '        <option value="pm" ' . $pm_selected . '>' . $locale_pm . '</option>' . PHP_EOL;
     883                $meridiem_drop_down .= '</selected>' . PHP_EOL;
     884        }
     885       
    867886        printf(_c('%1$s%2$s, %3$s <br />@ %4$s : %5$s|1: month input, 2: day input, 3: year input, 4: hour input, 5: minute input'), $month, $day, $year, $hour, $minute);
     887       
     888        if ( '12_hour' == $time_format )
     889                echo $meridiem_drop_down;
     890       
    868891        echo "\n\n";
    869892        foreach ( array('mm', 'jj', 'aa', 'hh', 'mn') as $timeunit )
    870893                echo '<input type="hidden" id="hidden_' . $timeunit . '" name="hidden_' . $timeunit . '" value="' . $$timeunit . '" />' . "\n";
     894
     895        if( '12_hour' == $time_format )
     896                echo '<input type="hidden" id="hidden_meridiem" name="hidden_meridiem" value="' . $meridiem . '" />';
     897
    871898?>
    872899
    873900<input type="hidden" id="ss" name="ss" value="<?php echo $ss ?>" size="2" maxlength="2" />
     901<input type="hidden" id="time_format" name="time_format" value="<?php echo $time_format; ?>" />
    874902<?php
    875903}
    876904
  • wp-admin/includes/user.php

     
    8080        else
    8181                $user->admin_color = 'fresh';
    8282
     83        if ( !update )
     84                $user->time_format = '24_hour'; // Default to 24 hour time for new users.
     85        else if ( isset( $_POST['time_format'] ) )
     86                $user->time_format = $_POST['time_format'];
     87        else
     88                $user->time_format = '24_hour';
     89               
    8390        $errors = new WP_Error();
    8491
    8592        /* checking that username has been typed */
  • wp-admin/user-edit.php

     
    161161        </tr>
    162162<?php endif; ?>
    163163<tr>
     164<th scope="row"><?php _e('Editor Time Format')?></th>
     165<td>
     166        <?php
     167                $current_time_format = get_user_option('time_format', $user_id);
     168                if ( !empty($current_time_format) && $current_time_format != '24_hour' && $current_time_format != '12_hour' )
     169                        $current_time_format = '24_hour';
     170        ?>
     171        <label for="time_format_12"><input name="time_format" id="time_format_12" type="radio" value="12_hour" <?php checked('12_hour', $current_time_format);?> />12-hour <i>2:35pm</i></label><br />
     172        <label for="time_format_24"><input name="time_format" id="time_format_24" type="radio" value="24_hour" <?php checked('24_hour', $current_time_format);?> />24-hour <i>14:35</i></label><br />
     173</td>
     174</tr>
     175<tr>
    164176<th scope="row"><?php _e('Admin Color Scheme')?></th>
    165177<td>
    166178<?php
  • wp-includes/registration.php

     
    9191 * 'jabber' - User's Jabber account.
    9292 * 'aim' - User's AOL IM account.
    9393 * 'yim' - User's Yahoo IM account.
     94 * 'time_format' - A string used to set the user's chosen time format for the editor (12_hour or 24_hour)
    9495 *
    9596 * @since 2.0.0
    9697 * @uses $wpdb WordPress database layer.
     
    157158        if ( empty($admin_color) )
    158159                $admin_color = 'fresh';
    159160        $admin_color = preg_replace('|[^a-z0-9 _.\-@]|i', '', $admin_color);
    160 
     161       
     162        if ( empty($time_format) ) {
     163                $time_format = '24_hour';
     164        } else {
     165                if ( $time_format != '12_hour' && $time_format != '24_hour' ) {
     166                        $time_format = '24_hour';
     167                }
     168        }
     169       
    161170        if ( empty($user_registered) )
    162171                $user_registered = gmdate('Y-m-d H:i:s');
    163172
     
    181190        update_usermeta( $user_id, 'yim', $yim );
    182191        update_usermeta( $user_id, 'rich_editing', $rich_editing);
    183192        update_usermeta( $user_id, 'admin_color', $admin_color);
     193        update_usermeta( $user_id, 'time_format', $time_format);
    184194
    185195        if ( $update && isset($role) ) {
    186196                $user = new WP_User($user_id);