Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP's DateTime::Diff gets it wrong?
    text
    copied!<p>DateTime::Diff should calculate a proper interval and take into account Daylight Savings Time (DST) and leap years. Although apparently it isn't so. Code of horror:</p> <pre><code>$d1 = new DateTime("2011-10-30 01:05:00", new DateTimeZone("Europe/Stockholm")); $d2 = new DateTime("2011-10-30 03:05:00", new DateTimeZone("Europe/Stockholm")); echo $d1-&gt;getOffset() / (60 * 60); </code></pre> <p>Prints '2'! Keep in mind thus that UTC time = 1h - 2h = 23:05:00 the day before.</p> <pre><code>echo $d2-&gt;getOffset() / (60 * 60); </code></pre> <p>Prints '1'. DST happened. UTC time = 3h - 1h = 02:05:00.</p> <pre><code>$di = $d1-&gt;diff($d2); echo "Hours of DateInterval: " . $di-&gt;h; </code></pre> <p>Prints '2'! Wrong?</p> <pre><code>$hoursofdiff = ($d2-&gt;getTimeStamp() - $d1-&gt;getTimeStamp()) / 60 / 60; echo "Calculated difference in hours: $hoursofdiff"; </code></pre> <p>Prints '3'! Correct?</p> <p>When the clock turned 03:00:00 at the given date, all Swedes turned their clock back one hour to 02:00:00. That means that total amount passed between 01:05 until 03:05 is three hours, much like the manual calculation echo'ed when using the UNIX TimeStamp. And much like we calculates on our fingers if we use an analogue clock. Even more so when we calculate the difference between the two UTC timestamps I got using PHP's own logic of offsets (!).</p> <p>Is it PHP or have my brain ceased to work properly? A reprimand from anyone of you all gods that exist on this site would make me so happy!</p> <p>I'm using PHP 5.4 (VC9) on an Apache-server. Unfortunately I use Windows 7 x64 as OS. I have tested my setup against all claims of bugs in PHP's Date/Time classes (there are a couple related to Windows) and can confirm that my system have none of them. Except from the above stated code I have not found any other errors. I pretty much validated all code and output the book "PHP Architect's guide to date and time programming" had to offer. Therefore I must conclude it has to be my brain witch has defaulted but I thought I'd give it a shoot here first.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload