Question

I am using this code:

//get days
$a = date_create($arrive);
$d = date_create($leave);
//$days = date_diff($d,$a);

echo '<input type="hidden" id="days" value="'.date_diff($d,$a).'"/>';

and getting this error: Catchable fatal error: Object of class DateInterval could not be converted to string

I do not know how to fix the issue.

Was it helpful?

Solution

date_diff() returns a DateInterval object which you need to call format() on to get the actual value from it. In this case you will use %d to get the number of days.

//get days
$a = date_create($arrive);
$d = date_create($leave);
$diff = date_diff($d,$a);

echo '<input type="hidden" id="days" value="'.$diff->format("%d").'"/>';

Just keep in mind this will only go up to 31. From there you will need to use %m with %d to also display months.

See it in action

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top