【发布时间】:2012-11-21 20:03:01
【问题描述】:
我正在开发一个日历应用程序。在此,来自不同时区的用户创建/查看事件。 我正在考虑按照以下方法将事件时间保存为 UTC 并以用户的本地时间显示。注意:用户有他们喜欢的时区设置。
将事件开始时间保存为 UTC 时间戳:
$timezone = new DateTimeZone( $user_timezone_name );
$utcOffset = $timezone->getOffset( new DateTime( $event_start_time_string_local ) );
$event_start_timestamp_local = maketime( $event_start_time_string_local );
//Now add/subtract $utcOffset to/from $event_start_timestamp_local to get event's start //time timestamp in UTC and save this result in DB.
获取用户时区的事件开始时间:
date_default_timezone_set( $user_timezone_name );
$eventTimeLocalTime = date("Y-m-d H:i:s", $event_start_timestamp_in_UTC );
地点:
user_timezone_name is user's timezone setting.
event_start_time_string_local is event's start time string in local/civil time.
event_start_timestamp_in_UTC is event's start time timestamp in UTC.
我的问题:
- 上面使用的 PHP API 是否负责所有地区的夏令时?
- DST 时间本身在不同年份会发生变化,PHP 是如何获取这方面的信息的?我们需要升级 PHP 吗?如果是这样,我们是否需要升级所有或特定的 PHP 库?
参考资料: - does-phps-date-default-timezone-set-adjust-to-daylight-saving - get-timezone-offset-for-a-given-location
【问题讨论】:
标签: php datetime utc dst timezone-offset