简体   繁体   中英

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM