I want to find the difference between two dates and I have used date_diff
for the same. When format function is applied on date_diff
object it returns an error.
Call to a member function format() on boolean
$field_value
is fetched from the database and it's format is dd/mm/YYYY
. When I hard-code the values for $field_value
and $indexing_value
the following code works.
Everything is running fine till line number 8. I have tried outputting the value of
$diff->format("%R%a")
and it is returning exact value but the code gives error near the if statement.
$date = new DateTime();
$current_date = $date->format('d/m/Y');
$indexing_value = str_replace("/", "-", $field_value);
$current_value = str_replace("/", "-", $current_date);
$indexing_value = date_create($indexing_value);
$current_value = date_create($current_value);
$diff = date_diff($indexing_value, $current_value);
if ($diff->format("%R%a") < 0) {
echo "1";
} else {
echo "2";
}
Please let me know what is wrong with the above code.