0

Possible Duplicate:
How do you handle timezone difference calculation in PHP?

On yesterday's change to DST in Sweden, I run into a issue with some code I made. I'm using the PHP DateTime, DateTimeZone, DateTimePeriod etc, and do not agree with the results.

What is your opiontion on number of days between these dates, and the number of hours ?

"Sun, 28 Oct 2012 01:00:00 +0200"
"Mon, 29 Oct 2012 00:00:00 +0100"

PHP tells me it is 0 days and 23 hours. I would say it is 0 days, but 24 hours.

Update: The timezone is "Europe/Stockholm", so it is the same for both dates.

To reproduce:

$timezone = new DateTimeZone("Europe/Stockholm");
$date1 = new DateTime("Sun, 28 Oct 2012 01:00:00", $timezone);
$date2 = new DateTime("Mon, 29 Oct 2012 00:00:00", $timezone);
$date1->setTimezone($timezone);
$date2->setTimezone($timezone);
$diff = $date1->diff($date2);
var_dump($diff);

Update 2:

Using Java and JodaTime class, it gives me 24 hours. So appearently the correct answer is not obvious :)

Community
  • 1
  • 1
luttkens
  • 1,272
  • 8
  • 16
  • 1
    This site is not for collecting opinions. Also: Please show the code you use to make PHP tell you there are 23 hours between those timestamps. – hmakholm left over Monica Oct 29 '12 at 10:25
  • Updated with code to reproduce. I know PHP has some bugs/unexpected behavoirs when it comes to handling DST, and that's why I was asking for what it should be. May opionions is a bad word, but would you say this is a bug? Or is the results expected? – luttkens Oct 29 '12 at 10:46

2 Answers2

1

You're dealing with daylight saving time.

As you know, in October, we have an extra hour inserted into the clock at overnight. At the end of DST the hour between midnight an 1AM is repeated. This means that on Sunday 28th October the time "1am" is ambiguous. There were two of them. 1am happened twice.

This means there there simply isn't an accurate answer for "how many hours were there between 1am on sunday and any other given time." There are two valid answers. PHP is giving you one, but you're expecting the other.

As long as you're using local timezones to calculate time, this problem is unresolveable. The only solution is to always use UCT for all date calculations and date storage, and only switch to the local timezone when you are actually displaying the time to the user.

SDC
  • 14,192
  • 2
  • 35
  • 48
  • Is there any standards or recommendations on how to handle this ambiguity? – luttkens Oct 29 '12 at 10:49
  • @luttkens - well, they have different timezone names. So for example, here in the UK the first hour of midnight to 1am was in BST (British Summer Time) and the second hour was in GMT (Greenwich Mean Time). So if you can specify which one you meant then there's no ambiguity. But I don't know how you would do that. You could use `DatTimeZone::getOffset()` or `getTransitions()` to find out what PHP thinks your offset is for a given time; that might help, but it would be quite a lot of effort compared with just using UCT. – SDC Oct 29 '12 at 11:05
  • Using "Europe/Stockholm" as a timezone there is actually an amiguity, since it can be either during summer time or winter time. I can check the timestamp difference, and that would give me number days I expect. But I was just curious if 23 hours is correct. Anyway, thanks for discussing! – luttkens Oct 29 '12 at 15:01
0
$time = strtotime("Mon, 29 Oct 2012 00:00:00 +0100")-strtotime("Sun, 28 Oct 2012 01:00:00 +0200");
$days=$time/(24*60*60);
echo $days; 
Man Programmer
  • 5,300
  • 2
  • 21
  • 21