0

I have found various methods to calculate the number of dates between 2 dates by first using date_create() and passing it the dates (without time) and then using ->diff on one of the them and works perfectly fine.

However when I attempt to do some integer related operations on it, such as comparing it if difference is >0 then it shows an error :

Object of class DateInterval could not be converted to integer

I have used var_dump() and it shows the value in it stores as string, but intval() is also not successful over it. Any other way to change it to integer?

Code is as follows:

$d1 = date_create(); //get current server time
$d2 = date_create("2020-02-01");//some future date
$diff = $d1->diff($d2);

echo $diff->days; //works fine & displays result perfectly
if($diff->days > 30) {echo "month of difference";} //error here
Lincoln
  • 165
  • 1
  • 12

1 Answers1

0

How about using this in order to convert into integer

$diffDays= intval($diff->format("%d"));
if($diffDays > 30) {echo "month of difference";} //error here probably
Dimitrios Desyllas
  • 9,082
  • 15
  • 74
  • 164