0
$duedate = date("Y")."-".$row['due_date'];     //$row['due_date'] = 08-23
$submitdate= $row['sub_date'];     //2016-08-29
$diff = date_diff($duedate,$submitdate);
echo $diff->format("%d days");

Error 1 = date_diff() expects parameter 1 to be DateTimeInterface, string given.....

Error 2 = Call to a member function format() on boolean in.....

Aaqib Mehran
  • 41
  • 1
  • 6

2 Answers2

2

date_diff function requires a DateTime object to be passed into it.

Using the DateTime::createFromFormat function you're able to create a DateTime object from a string, then use it in date_diff.

(This code is not tested and i'm quite new to PHP as well, i hope this helps you!)

//Create the date from our records and the current year.
$dueDate = DateTime::createFromFormat('Y-m-d', date("Y")."-".$row['due_date']); //$row['due_date'] = 08-23
$submitDate= DateTime::createFromFormat('Y-m-d', $row['sub_date']);     //2016-08-29

$diff = $dueDate->diff($submitDate); //Calculate the difference between the two dates

//Echo it out
echo $diff->format('%R%a days');
Mark Phillips
  • 273
  • 1
  • 8
0

Your code will not work unless you pass DateTime object to it. Also this can be done using diff method on DateTime objects. You can check examples in documentation.

Also, check between what dates you want the difference because it can be both positive and negative 6 days.

$duedate = new DateTime(date("Y") . "-" . $row['due_date']);
$submitdate = new DateTime($row['sub_date']);
echo $duedate->diff($submitdate)->format("%R%a days");

Demo

Iurii Tkachenko
  • 3,106
  • 29
  • 34