Compare 2 dates [duplicate]

0

I'm trying to make this code work correctly, but the same thing I noticed only compares the day because if it's bigger than the current day works correctly, but if it's shorter than the current day it has the error "Your account has expired" it is as if he did not compare the month nor the year! But in case the other topics do not serve me because they are to compare 2 different dates that is stored in the variable, my date is from the database!

$vencimento = $dado['vencimento'];

if (date("d/m/Y") > $vencimento) {

        echo "A sua conta expirou";

    }
else {

        echo "A sua conta ainda não expirou";

}
    
asked by anonymous 25.02.2018 / 07:50

1 answer

1

You could do as follows, using the native PHP DateTime class:

<?php

$vencimento = DateTime::createFromFormat('d/m/Y', '22/02/2018');
$hoje = new DateTime();

if ($hoje > $vencimento) {
    echo "A sua conta expirou";
} else {
    echo "A sua conta ainda não expirou";
}
    
25.02.2018 / 14:48