0
    $date1 = "2018-10-10";
    $date2 = "2018-11-19";

    $date1 = date_create($date1);
    $date2 = date_create($date2);

    $diff = date_diff($date1,$date2);

    echo $diff;

When I try to output $diff, it gives me this error:

Recoverable fatal error: Object of class DateInterval could not be converted to string in C:\xampp\htdocs\grab.php on line 29

Help?

michaelmcgurk
  • 6,367
  • 23
  • 94
  • 190
  • 2
    use `var_dump($diff);` to see what it contains, and check the [DateInterval](https://www.php.net/manual/en/class.dateinterval.php) documentation – aynber Aug 21 '19 at 15:59

4 Answers4

1

How about using this to echo out the difference between your dates:

echo $diff->format("%R%a days");

instead of just

echo $diff;
Devmix
  • 1,599
  • 5
  • 36
  • 73
1

call DateInterval::format() to get the date difference in string.

echo $diff->format('%d days');

you can also read the manual: https://www.php.net/manual/en/dateinterval.format.php

sid
  • 365
  • 2
  • 11
1

date_diff doesn't return a string but an object which represents the difference. You need call the format method on that object.

https://www.php.net/manual/en/datetime.diff.php#refsect1-datetime.diff-examples

$date1 = "2018-10-10";
$date2 = "2018-11-19";

$date1 = date_create($date1);
$date2 = date_create($date2);

$diff = date_diff($date1,$date2);
echo $diff->format('%R%a days');
waterloomatt
  • 3,662
  • 1
  • 19
  • 25
1

try:

echo $diff->format("%a");
Manzolo
  • 1,909
  • 12
  • 13