timezone with DST handling by PHP
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:
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.
Yes php api takes into account DST. Most of the time you can safely perform time conversion the way deceze had shown. The problem is that it is not always possible/doable or makes sense. Consider below example - in Poland (in 2013) on Oct 27th at 3am your clock is moved back one hour to switch to standard time (offset UTC+01:00) on March 31st at 2am clock is adjusted forward by 1h to switch to summer time(offset UTC+02:00). Let's now see how 'real time' ticks in Grenwich/UTC and how conversion by PHP from Europe/Warsaw to UTC works:
Europe/Warsaw time offset UTC php2utc conversion php offset
2013-10-27 01:00:00 +2 2013-10-26 23:00:00 2013-10-26 23:00:00 +2
2013-10-27 01:30:00 +2 2013-10-26 23:30:00 2013-10-26 23:30:00 +2
2013-10-27 02:00:00 +2 2013-10-27 00:00:00 2013-10-27 01:00:00 +1 *
2013-10-27 02:30:00 +2 2013-10-27 00:30:00 2013-10-27 01:30:00 +1 *
2013-10-27 02:59:00 +2 2013-10-27 00:59:00 2013-10-27 01:59:00 +1 *
3am -> 2am .....................................summer time changes to standard(winter) time @3am we subtract 1h so 3am becomes 2am
2013-10-27 02:00:00 +1 2013-10-27 01:00:00 2013-10-27 01:00:00 +1
2013-10-27 02:30:00 +1 2013-10-27 01:30:00 2013-10-27 01:30:00 +1
2013-10-27 03:00:00 +1 2013-10-27 02:00:00 2013-10-27 02:00:00 +1
2013-10-27 03:30:00 +1 2013-10-27 02:30:00 2013-10-27 02:30:00 +1
As you can see between 2am-3am there is no way PHP can know 'which' hour (from before/after adjustment) it is hence cannot reliably convert. Hope this helps someone in his/her deliberations ;-)
链接地址: http://www.djcxy.com/p/29412.html下一篇: 时区以及PHP处理DST的时区