3

I have an issue with DateInterval::$d property with march 2016.

See the code:

$date1 = new \DateTime('2016-07-01');
$date2 = new \DateTime('2016-07-31');

$diff = date_diff($date1, $date2, true);

var_dump($diff->d); // int(0) => OK

$date1 = new \DateTime('2016-03-01');
$date2 = new \DateTime('2016-03-31');

$diff = date_diff($date1, $date2, true);

var_dump($diff->d); // int(1) => NOT OK

On the second example, I have 1 month difference (that is normal) and 1 extra day.

I don't understand at all why and I don't have this issue with another 31 days' month.

Is that a PHP bug or a bad usage of this property?

Soullivaneuh
  • 3,553
  • 3
  • 37
  • 71
  • I get `int(30) int(30)` – j08691 Mar 01 '16 at 18:22
  • @j08691 with which php vers? me also obtain OP result. Note that the cmd is `->d`, not `->days` – fusion3k Mar 01 '16 at 18:26
  • @fusion3k 5.6 or 7. http://sandbox.onlinephpfunctions.com/code/815db837b847c7315a87903b0b2c0a7c4b01f799 – j08691 Mar 01 '16 at 18:28
  • 1
    The problem is TimeZone related, but I don't know the reason. March is the Daylight Saving Time month in most parts of the world, and this could maybe explain the additional day (but... really?). Anyway, I don't understand why `->m` is 1: actually, the correct result should be 0 month, 30 days. – fusion3k Mar 01 '16 at 18:45
  • @fusion3k is right (try to use 2016.11.01-30 - you will get 29 days). But still it strange. Why if you var_dump object it returns 0 hours for $diff->h. Shouldn't it consider daylight saving if days and months are doing it? – Jakub Mar 01 '16 at 18:56
  • @Jakub I have tested with many (not all) TimeZone. It seems affect only positive tz (GMT+1, GMT+2, etc) – fusion3k Mar 01 '16 at 19:03
  • See [this](http://php.net/manual/en/datetime.diff.php#101990) and [this](http://stackoverflow.com/a/24931746/3294262) – fusion3k Mar 01 '16 at 19:43
  • I opened a PHP issue just in case: https://bugs.php.net/bug.php?id=71700 – Soullivaneuh Mar 02 '16 at 09:11

0 Answers0