Compare PHP date and time

Asked

Viewed 4,120 times

1

I have the following dates:

data atual: 2018-02-11 20:14:40
data expiracao: 2018-02-12 19:57:10

I need to make a comparison between one and the other.

if($dados['transacao']->data_expira <= date("Y-m-d H:i:s")){
    echo "Ainda está dentro do prazo";
} else {
    echo "Você não pode mais efetuar o pagamento desta cobrança.";
}

Logically, I would be right, but it doesn’t work. He insists on telling me I’m past my deadline. I’ve been using the example posted here in this SPO link:

How to compare dates in PHP

What can I do to adjust?

  • 1

    Utilize strtotime($dados['transacao']->data_expira) <= time()

  • @Rpgboss Exactly why I put that I’ve used the example and it didn’t work.

  • Bring with echo or print_r the result $data['transaction']->data_expires to see if it really is a date or a string.

  • 1

    what returns this: $data['transaction']->data_expires?

  • @Virgilionovic returns the expiration date, in this format: string(19) "2018-02-12 19:57:10"

1 answer

4


it’s quite simple actually

$data_transacao= strtotime($dados['transacao']->data_expira);
$hoje= strtotime(date("Y-m-d H:i:s"));

if($data_transacao<= $hoje) { 
   echo "Ainda está dentro do prazo"; 
} else {
   echo "Você não pode mais efetuar o pagamento desta cobrança.";
}
  • So, oh that’s, it didn’t work... as simple as it is, it didn’t work, and the format of the two is like string.. : string(19) "2018-02-12 19:57:10" string(19) "2018-02-12 14:25:52"

  • 1

    so I’m turning it into date using strtotime. I just tested it here and everything ok.

Browser other questions tagged

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