WordPress在哪里获得夏令时信息

时间:2017-09-24 04:54:26

标签: php utc dst timezone-offset

我的问题是,WordPress从哪里获取与自动时区和DST更改相关的数据,以及如何实现相同的主体将用户选择的本地日期和时间的Web应用程序转换为存储的偏移量以允许其移回在夏令时期间转发时钟?

显然我可以将其存储为UTC,然后将时间转换为他们当地的时区,但如果他们处于相反的夏令时设置,那么这仍然无法准确反映他们输入的日期和时间。创建。我正在使用带有sql server的asp.net c#。

WordPress通过选择城市自动反映夏令时,而不是WP-admin中的偏移小时数>设置>一般>时区。然后,您将看到如下通知:

This timezone is currently in daylight saving time.

Standard time begins on: Sunday 25.10. 04:00,

它位于/wp-admin/options-general.php的代码中

<?php
$current_offset = get_option('gmt_offset');
$tzstring = get_option('timezone_string');

$check_zone_info = true;

// Remove old Etc mappings. Fallback to gmt_offset.
if ( false !== strpos($tzstring,'Etc/GMT') )
$tzstring = '';

if ( empty($tzstring) ) { // Create a UTC+- zone if no timezone string exists
$check_zone_info = false;
if ( 0 == $current_offset )
    $tzstring = 'UTC+0';
elseif ($current_offset < 0)
    $tzstring = 'UTC' . $current_offset;
else
    $tzstring = 'UTC+' . $current_offset;
}

?>
<th scope="row"><label for="timezone_string"><?php _e('Timezone') ?></label></th>
<td>

<select id="timezone_string" name="timezone_string" aria-describedby="timezone-description">
<?php echo wp_timezone_choice( $tzstring, get_user_locale() ); ?>
</select>

<p class="description" id="timezone-description"><?php _e( 'Choose either a city in the same timezone as you or a UTC timezone offset.' ); ?></p>

<p class="timezone-info">
<span id="utc-time"><?php
    /* translators: 1: UTC abbreviation, 2: UTC time */
    printf( __( 'Universal time (%1$s) is %2$s.' ),
        '<abbr>' . __( 'UTC' ) . '</abbr>',
        '<code>' . date_i18n( $timezone_format, false, true ) . '</code>'
    );
?></span>
<?php if ( get_option( 'timezone_string' ) || ! empty( $current_offset ) ) : ?>
<span id="local-time"><?php
    /* translators: %s: local time */
    printf( __( 'Local time is %s.' ),
        '<code>' . date_i18n( $timezone_format ) . '</code>'
    );
?></span>
<?php endif; ?>
</p>

<?php if ( $check_zone_info && $tzstring ) : ?>
<p class="timezone-info">
<span>
<?php
// Set TZ so localtime works.
date_default_timezone_set($tzstring);
$now = localtime(time(), true);
if ( $now['tm_isdst'] )
    _e('This timezone is currently in daylight saving time.');
else
    _e('This timezone is currently in standard time.');
?>
<br />
<?php
$allowed_zones = timezone_identifiers_list();

if ( in_array( $tzstring, $allowed_zones) ) {
    $found = false;
    $date_time_zone_selected = new DateTimeZone($tzstring);
    $tz_offset = timezone_offset_get($date_time_zone_selected, date_create());
    $right_now = time();
    foreach ( timezone_transitions_get($date_time_zone_selected) as $tr) {
        if ( $tr['ts'] > $right_now ) {
            $found = true;
            break;
        }
    }

    if ( $found ) {
        echo ' ';
        $message = $tr['isdst'] ?
            /* translators: %s: date and time  */
            __( 'Daylight saving time begins on: %s.')  :
            /* translators: %s: date and time  */
            __( 'Standard time begins on: %s.' );
        // Add the difference between the current offset and the new offset to ts to get the correct transition time from date_i18n().
        printf( $message,
            '<code>' . date_i18n(
                __( 'F j, Y' ) . ' ' . __( 'g:i a' ),
                $tr['ts'] + ( $tz_offset - $tr['offset'] )
            ) . '</code>'
        );
    } else {
        _e( 'This timezone does not observe daylight saving time.' );
    }
}
// Set back to UTC.
date_default_timezone_set('UTC');
?>
</span>

1 个答案:

答案 0 :(得分:1)

$now = localtime(time(), true);
if ( $now['tm_isdst'] )
    _e('This timezone is currently in daylight saving time.');
else
    _e('This timezone is currently in standard time.');

我会使用localtime函数,无论来自哪个库。

- edit-- 我还会注意到这些界限:

// Set TZ so localtime works.
date_default_timezone_set($tzstring);

那些可能是必要的,我不知道,我不写PHP。