Compare 2 dates

Asked

Viewed 1,187 times

0

I am trying to make this code work properly, but the same as I realized only compares the day because if it is larger than the current day works correctly, but if it is smaller than the current day presents the error "Your account has expired" It’s like he doesn’t compare month or year! But in case the other topics do not suit me as they are to compare 2 different dates that this 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";

}

1 answer

1


You could do it 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";
}
  • but in case my date would be the database in case: $maturity = $given['maturity'];

  • Just put it in the format that is coming from the database in the parameters of the Datetime method::createFromFormat('format', '$given['maturity']');

  • Solved, thank you, for those who want to use my code if they are in trouble, this ai: $expiration = $given['expiration']; $expiration1 = Datetime::createFromFormat(’d/m/Y', $expiration); if ($today > $expiration1) { echo "...";&#Xa a;}

Browser other questions tagged

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