How to resolve the problem Call to a Member Function diff() on string in

Asked

Viewed 1,254 times

0

I am finding a flaw when I try to compare a date that is in the database with the current date, returns me the fault Call to a Member Function diff() on string in

I take the date from the database and when I give an echo appears correct.

$dbDate = $this->Query->fetchColumn();

When I try to generate the current date for later comparison generates the failure.

$aDate = date('Y-m-d H:i:s');

The fault log generated on the server points to the line below when trying to check the defiance.

$timePassed = $dbDate->diff($aDate);

I need to know how many minutes have passed since the date it’s in the database.

$minutes  = $timePassed->days * 24 * 60;
$minutes += $timePassed->h * 60;
$minutes += $timePassed->i;
  • By the looks of $dbDate probably a string. Something you can confirm by doing var_dump($dbDate);

1 answer

2


It is necessary to convert the $aDate on object DateTime:

$aDate = new DateTime(date('Y-m-d H:i:s'));

As well as the date coming from the bank:

$dbDate = new DateTime($dbDate);

Thus remaining:

$dbDate = new DateTime($dbDate);
$aDate = new DateTime(date('Y-m-d H:i:s'));
$timePassed = $dbDate->diff($aDate);
$minutes  = $timePassed->days * 24 * 60;
$minutes += $timePassed->h * 60;
$minutes += $timePassed->i;

Check it out at Ideone

  • I made the changes and it worked. Thank you!!

  • Solved my problem too, 4 years later, thank you! =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.