How to solve the problem Call to a member function diff () on string in

0

I am encountering a crash when I try to compare a date that is in the database with the current date, it returns me to crash Call to a member function diff () on string in

I get the date of the database and when I give an echo it appears correct.

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

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

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

The fault log generated on the server points to the line below when it tries to check the fault.

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

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

$minutes  = $timePassed->days * 24 * 60;
$minutes += $timePassed->h * 60;
$minutes += $timePassed->i;
    
asked by anonymous 27.11.2017 / 22:23

1 answer

1

You must convert $aDate to object DateTime :

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

As well as the date from the bank:

$dbDate = new DateTime($dbDate);

Looking like this:

$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;

See Ideone

    
28.11.2017 / 00:57