php failed to parse time string


I want to get difference between a date in db and the current date

That is my code :

 $current_date = new \DateTime(date('Y-m-d H:i:s'));
    foreach ($notifs as $notif) {
        $created_notif = new \DateTime($notif->created);
        $diff = date_diff($current_date, $created_notif);
                if ($diff->y > 0) {
                    $notif->time = $diff->y . " year";

And this is the error that i got :

DateTime::__construct(): Failed to parse time string (17/05/2016 08:54) at position 0 (1):

And to don't say that i havent searched i tiriedDateTime::createFromFormat

Some thing like this :

$format = 'Y-m-d H:i:s';
            $current_date = new \DateTime(date($format));
            foreach ($notifs as $notif) {
                $created_notif =\DateTime::createFromFormat($format,$notif->created);
                $diff = date_diff($current_date, $created_notif);//line 32
 debug($created_notif);//line 33

That's what i got :

Warning (2): date_diff() expects parameter 2 to be DateTimeInterface, boolean given [APP/Controller\AdminController.php, line 32]

\src\Controller\AdminController.php (line 33)


Please i need your help




I assume, the$notif->created is17/05/2016 08:54 so at that point you should use

$created_notif = \DateTime::createFromFormat('d/m/Y H:i', $notif->created);

This is why you not get aDateTime object and this is whydate_diff not works.

People are also looking for solutions to the problem: php - Woocommerce remove order item meta


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.