时区用PHP处理DST [英] timezone with DST handling by PHP

查看:142
本文介绍了时区用PHP处理DST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理日历应用程序。在不同时区的用户创建/查看事件。
我正在考虑按照以下方法在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);
//现在添加/减去$ utcOffset到/从$ event_start_timestamp_local以获取事件的开始//时间戳在UTC中,并将此结果保存在DB中。

要在用户时区获取活动开始时间:

  date_default_timezone_set($ user_timezone_name); 
$ eventTimeLocalTime = date(Y-m-d H:i:s,$ event_start_timestamp_in_UTC);

其中:

 code> user_timezone_name是用户的时区设置。 
event_start_time_string_local是本地/民用时间的事件开始时间字符串。
event_start_timestamp_in_UTC是以UTC为单位的事件开始时间戳。

我的问题:




  • 上面使用的PHP API是否适用于所有地区的DST?

  • DST时间本身在不同年份发生变化,PHP如何获取信息对这个?我们需要升级PHP吗?如果是这样,我们需要升级所有或特定的PHP库?



参考:
- does-phps-date-default-timezone-set-adjust-to -daylight-saving
- 获取-timezone-offset-for-a-given-location

解决方案

你太过于简单了。要使用 DateTime 在两个时区之间进行转换,请执行以下操作:

  date_default_timezone_set 亚洲/加尔各答); //您的时区,服务器

$ date = new DateTime($ input,new DateTimeZone('Asia / Tokyo')); // USER's timezone
$ date-> setTimezone(new DateTimeZone('UTC'));
echo $ date-> format('Y-m-d H:i:s');

这将从用户的本地时区转换为UTC。为了在用户本地时间显示时间,交换两个时区。



是的,PHP负责处理DST。必要的转换规则是PHP安装的一部分。您可以通过更新PHP或更新timezonedb来保持最新状态: http://pecl.php.net/package/timezonedb


I am working on a calendar application. In this users from different time-zones create/view events. I am thinking to follow below method to save event time in UTC and display them in user's local time. Note: user has their preferred timezone setting.

To save event start time as UTC timestamp:

$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.

To get event start time in user's timezone:

date_default_timezone_set( $user_timezone_name );
$eventTimeLocalTime = date("Y-m-d H:i:s", $event_start_timestamp_in_UTC );

Where:

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.

My questions:

  • Whether the PHP APIs used above take care of DST for all regions?
  • The DST time itself changes in different years, how does PHP gets information about this? do we need to upgrade PHP? If so do we need to upgrade all or particular PHP library?

References: - does-phps-date-default-timezone-set-adjust-to-daylight-saving - get-timezone-offset-for-a-given-location

解决方案

You're way too overcomplicating this. To convert between two timezones using DateTime, do this:

date_default_timezone_set('Asia/Kolkata'); // YOUR timezone, of the server

$date = new DateTime($input, new DateTimeZone('Asia/Tokyo')); // USER's timezone
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('Y-m-d H:i:s');

This converts from a user's local timezone to UTC. To go the other way around, to display the time in the user's local time, swap the two timezones.

Yes, PHP takes care of DST. The necessary conversion rules are part of the PHP installation. You can keep them up to date by updating PHP, or by updating the timezonedb: http://pecl.php.net/package/timezonedb.

这篇关于时区用PHP处理DST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆