Abhijit Kumbhar Abhijit Kumbhar - 10 days ago 6
PHP Question

How to calculate difference of two dates in days, If dates are in string format in PHP

I have two date variables:

$dnow = "2016-12-1";
$dafter = "2016-12-11";


I want to calculate the difference of this two dates which are in string format so how do I calculate?
I used

date_diff($object, $object2)


but it expecting two date object, and I have dates in String format
, After using
date_diff
I get following error


Message: Object of class DateInterval could not be converted to string.

Answer

Try thsi,

$dnow = "2016-12-1";
$dafter = "2016-12-11";
$date1=date_create($dnow);
$date2=date_create($dafter);
$diff=date_diff($date1,$date2);
print_r($diff);

DEMO