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

  • SEARCH
  • Home
  • 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 7541061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:47:48+00:00 2026-05-30T07:47:48+00:00

I am having a weird problem with PHP’s DateTime diff method. I’ve been using

  • 0

I am having a weird problem with PHP’s DateTime diff method. I’ve been using DateTime methods for some while now and thought I’ve got the hang of it, but this problem amazes me.

I am using the diff method to determine the number of months/days between two dates.
Today, I used the following code:

$start = new DateTime('2012-03-01');
$end = new DateTime('2012-03-31');

$period = $start->diff($end);

var_dump($period);

Which produced the following output:

object(DateInterval)[3]
  public 'y' => int 0
  public 'm' => int 1 // Shouldn't this be 0?
  public 'd' => int 1 // And where does this come from? Would expect 30.
  public 'h' => int 0
  public 'i' => int 0
  public 's' => int 0
  public 'invert' => int 0
  public 'days' => int 30

As my comment above says, where the hell does the 1m1d come from (I would expect 0m30d). I can’t for the life of me figure out where it gets this value from. I also tried putting the midnight timestamp 00:00:00 behind both dates to make sure it wasn’t a time thing, but that made no difference to the output.

Can anybody explain this outcome to me?
This problem occurs on my Debian development server, running PHP 5.3.3 with Xdebug 2.1.1 and Suhosin 0.9.32.1 installation.

Update

Reproduced this problem on my production server, which is on CentOS 6.0 with “raw” PHP 5.3.8 without any extensions/patches.

Update 2

I managed to find a “workaround” (only working at this moment in time). If I change the absolute dates to:

$start = new DateTime('first day of next month');
$end = new DateTime('last day of next month');

It does give 0m30d as output. Still, I’m stumped by the above, so if anyone has a clue about that, please let me know!

  • 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-30T07:47:50+00:00Added an answer on May 30, 2026 at 7:47 am

    Congratulations, you have just discovered DST! If you check with the DST switchover for your timezone, you will see that $start is actually getting Feb-29, and $end getting Mar-30. (2-3) = 1 (therefore, m=1), and (29-30) = 1 (therefore, d=1).

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

Sidebar

Related Questions

I'm having a weird problem with some JavaScript/DOM code I've been playing with. I'm
I'm having an extremely weird problem with a PHP script of mine. I'm uploading
I'm having the weird problem that after having javascript inject some dom-elements the css-rules
This is a really weird problem that I have been having. When I download
Having a weird problem with PHPUnit. We're using PHPUnit as part of a continuous
I'm having a weird problem with a threaded class using Boost::threads. Here is a
I'm having a weird problem using Spring and Hibernate. I started using Hibernate using
Im having this weird problem with creating Json dynamically.... for some reason this doesnt
I'm having this weird problem with HtmlUnit in Java. I am using it to
I'm having a weird problem on deployment while trying to use Microsoft Enterprise Library's

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.