0

When trying to check the difference between two dates I found that when getting the difference between the dates: 31/10/2019 and 01/12/2019.

I was only getting a result of one month. Anyone know how I can fix it?

$d1 = new DateTime('2019-10-31');
$d2 = new DateTime('2019-12-01');
$interval= $d1->diff($d2);
var_dump($interval);

Returns

object(DateInterval)#3 (16) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(1)
  ["d"]=>
  int(0)
  ["h"]=>
  int(0)
  ["i"]=>
  int(0)
  • 2
    Possible duplicate of [How to calculate the difference between two dates using PHP?](https://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – Can O' Spam Oct 31 '19 at 16:40

1 Answers1

1

Well your difference is 31 days, you can get the days like this.

<?php

$d1 = new DateTime('2019-10-31');
$d2 = new DateTime('2019-12-01');

$interval= $d1->diff($d2);

echo $interval->format('%R%a days');
Eli
  • 964
  • 9
  • 21