timezone with DST handling by PHP

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 ;-)


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.