Forums / Cotonti / Extensions / Support / DST Checking of Timestamps

Macik
#39451 2014-04-14 14:03

Thank you for a great question pushed me to read some docs. Here some info I get:

First of all — PHP already had tools to use timezines and DST.

Moreover, as DST is subject to change during long time periods (as legislation changes) PHP had these information in DateTimeZone::listAbbreviations()

Than PHP used this info to properly convert time from one time zone to another.

So two rules should be used: 
1. store timestamps in UTC (as you do)
2. use proper time zone for any convertion.

Let's try some example:

$date = new DateTime('2000-01-01', new DateTimeZone('Europe/Moscow'));  
// DST is used in these period  (+03:00 winter +04:00 summer)
$plugin_body .= $date->format('Y-m-d H:i:s P') . PHP_EOL; // 2000-01-01 00:00:00+03:00
$date->setTimeZone(new DateTimeZone('UTC'));
$plugin_body .= $date->format('Y-m-d H:i:s P') . PHP_EOL; // 1999-12-31 21:00:00+00:00
$date->modify('+ 13 year'); 
// in midsummer of 2011 DST was canceled in our region
// actually we get in DST time forever (+04:00 winter +04:00 summer)
$plugin_body .= $date->format('Y-m-d H:i:s P') . PHP_EOL; // 2012-12-31 21:00:00+00:00
$date->setTimeZone(new DateTimeZone('Europe/Moscow')); 
$plugin_body .= $date->format('Y-m-d H:i:s P') . PHP_EOL; // 2013-01-01 01:00:00+04:00
// as DST is cancelled we +04:00 for whole year

So if user had selected propper timezone in users settings (user profile) DST should be concerned.

But some problem may occurs only if you use time shift functions ($date->modify('+ 13 year');) in local time zone as real calculation is made in UTC.

Some info links:

 

 

https://github.com/macik
правильный хостинг — https://goo.gl/fjCa1F
This post was edited by Macik (2014-04-16 11:32, 10 years ago)