【发布时间】:2018-03-05 07:56:43
【问题描述】:
我的问题是,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>
【问题讨论】:
-
这些是你要找的机器人吗?
$current_offset = get_option('gmt_offset');$tzstring = get_option('timezone_string'); -
应该删除 C# 标签,我认为它在你的项目中只是切线,与实际问题无关。
-
嗨@IanRay,是的,那些可能是有罪的当事人,但它是如何运作的?它如何知道所需的偏移量以及该功能如何自动更改相应 DST 的日期和时间?我知道它可能是作为 CRON 的一部分处理的,只是不明白这个函数是如何工作的,并获取数据以反映新西兰奥克兰目前处于夏令时,澳大利亚墨尔本在特定日期开始是他们的,而布里斯班 AU 确实如此根本没有夏令时。
-
我大胆猜测一下,因为夏令时是一个没有外部变量的方程。 en.wikipedia.org/wiki/Template:Daylight_saving_in_time_zone/… 因此他们可以在后端实现它并且它总是有效的。他们只会查找用户通过 IP 信息访问的时区,而 Wordpress DateTime 库在显示“timezone_string”时可能会考虑到它。希望我已经回答了你的问题。
-
我认为这不是您要找的。
gmt_offset和timezone_string只是存储在数据库中的值。如果用户从 TZ 下拉列表中选择America/Vancouver,则America/Vancouver将存储在timezone_string中。如果他们从下拉列表中选择UTC-9:00,则-9将存储在gmt_offset中。
标签: php utc dst timezone-offset