Oldskool
Oldskool

Reputation: 34837

PHP DateTime diff weird result

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!

Upvotes: 2

Views: 1186

Answers (3)

TML
TML

Reputation: 12966

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

Upvotes: 4

Tom Will
Tom Will

Reputation: 361

The comments on this page (PHP manual) seem to suggest that the function can be quite buggy on certain platforms or with certain ranges of input.

This may well be the cause of your problem.

Upvotes: 0

Mathieu Dumoulin
Mathieu Dumoulin

Reputation: 12244

I don't get the same result as you and my result is actually ok...

object(DateInterval)#3 (8) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(0)
  ["d"]=>
  int(30)
  ["h"]=>
  int(0)
  ["i"]=>
  int(0)
  ["s"]=>
  int(0)
  ["invert"]=>
  int(0)
  ["days"]=>
  int(30)
}

Its not normal to get 1 month cause there ISNT even a month yet between the two dates, 1 month would be for

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

What i see here is you maybe got -1 day and 1 month instead of 30 days and you thought you saw 1 day 1 month, or the code you got is not what you pasted here...

Can you enlighten us?

Thanks

Upvotes: 1

Related Questions