James Green
James Green

Reputation: 49

Incorrect DateTime interval value php

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)

Upvotes: 0

Views: 75

Answers (1)

Eli
Eli

Reputation: 984

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');

Upvotes: 1

Related Questions