Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7645055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:50:31+00:00 2026-05-31T09:50:31+00:00

DateTime::Diff should calculate a proper interval and take into account Daylight Savings Time (DST)

  • 0

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:

$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->getOffset() / (60 * 60);

Prints ‘2’! Keep in mind thus that UTC time = 1h – 2h = 23:05:00 the day before.

echo $d2->getOffset() / (60 * 60);

Prints ‘1’. DST happened. UTC time = 3h – 1h = 02:05:00.

$di = $d1->diff($d2);
echo "Hours of DateInterval: " . $di->h;

Prints ‘2’! Wrong?

$hoursofdiff = ($d2->getTimeStamp() - $d1->getTimeStamp()) / 60 / 60;
echo "Calculated difference in hours: $hoursofdiff";

Prints ‘3’! Correct?

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 (!).

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!

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:50:32+00:00Added an answer on May 31, 2026 at 9:50 am

    You’re right, PHP currently doesn’t handle DST transitions…

    Bug reports #51051 (still open) and #55253 (fixed in PHP 5.3.9) describe the problems you’re having.

    Daniel Convissor has written an RFC trying to address the issue a while back but the change-logs don’t suggest this has been addressed. I was hoping this would fixed in 5.4 but I don’t see any evidence it has been.

    When/if it is implemented, it looks like you’ll have to append “DST” or “ST” to the time string.

    Best practice is to do all your date calculations in UTC, which avoids this problem.

    This DST best practices post is very informative too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using import datetime in python, is it possible to take a formatted time/date string
I tried using DateTime in PHP and used diff method to find the time
I have a Python datetime object that I want to convert to unix time,
I am having a weird problem with PHP's DateTime diff method. I've been using
Unfortunately I cannot use DateTime::Diff and am looking for a way to convert my
I found this snippet: def timesince(dt, default=just now): now = datetime.utcnow() diff = now
Consider the following test case: $start = new DateTime(2011-02-25); $end = new DateTime(2011-03-25); $interval
I've found a few problems in applications I've been doing where methods like DateTime::diff
Possible Duplicate: Converting string into datetime I am parsing an XML file that gives
I have a few lines of PHP that should work, but don't: $date1=new DateTime();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.