Because you may be causing error when comparing two dates in php

Asked

Viewed 54 times

1

I have the following line of code:

if($dataexpira < $dataLocal){ echo "Expirou data!";}

o That the following strings are stored in them:

if("04/02/2019 18:18:48" < "16/01/2019 18:18:48"){echo "Expirou data!"}

however he comparing in this way, he understands as Expired date, 'Cause you’re comparing whether the day is bigger and " not at all"...

how to compare your whole...

  • 1

    Use the class DateTime. Compare string will only work if it is in format Y-m-d H:i:s

  • Sorry I asked, but I can use these strings to transform into Datetime ?

2 answers

1


This form below will transform your string into a date object, and so you will make comparisons more securely by comparing numerical representations like timestamp.

<?php
// Objeto que representa a data expira.
$DataExpira = new DateTime();
$DataExpira->setTimestamp(strtotime("2019-02-16 18:18:48"));
// Objeto que representa a data local.
$DataLocal = new DateTime();
$DataLocal->setTimestamp(strtotime("2019-02-16 18:18:48"));
// Saí apenas para teste.
print "1 :: dataexpira :: " . $DataLocal->getTimestamp();
print "<br>";
print "2 :: dataLocal :: " . $DataExpira->getTimestamp();
print "<br>";
// Teste de datas.
if ($DataExpira->getTimestamp() < $DataLocal->getTimestamp()) {
    echo "Expirou data!";
} else {
    echo "NÃO Expirou data!";
}
?>

Doing so will look better your date representation.

  • Perfect friend ... this helped a lot !!!! thanks....

  • If at the end you will compare the timestamp, nor need to use class DateTime, the function strtotime already returns the value, just use it.

0

You do not need to do all this that was presented in the other reply. If it were to compare the timestamps just wouldn’t need to use the class DateTime.

$expira = strtotime("2019-02-16 18:18:48");
$local = strtotime("2019-02-16 18:18:48");

if ($expira < $local) {
    ...
}

But see that the date format has changed in this case, because the function strtotime does not recognize the standard you possess.

If you want to use DateTime, as the format "16/01/2019 18:18:48" is not an internationally recognized format, you need to specify as the class DateTime will process the given value; do this with the method DateTime::createFromFormat:

$expira = DateTime::createFromFormat("d/m/Y H:i:s", "04/02/2019 18:18:48");
$local = DateTime::createFromFormat("d/m/Y H:i:s", "16/01/2019 18:18:48");

Note that we define the format as "d/m/Y H:i:s" for PHP to know what each number is. In this case, the format is <dia>/<mês>/<ano com 4 dígitos> <hora no formato 24h>/<minutos>/<segundos>. The method is static and returns a new instance of DateTime.

Like DateTime is native to PHP (and written in C), it has implemented operator overload. That is, you can compare two objects DateTime with operators >, <, etc, normally. Therefore, to know if $expira is less than $local, just do:

if ($expira < $local) {

}

Thus, getting:

$expira = DateTime::createFromFormat("d/m/Y H:i:s", "04/02/2019 18:18:48");
$local = DateTime::createFromFormat("d/m/Y H:i:s", "16/01/2019 18:18:48");

if ($expira < $local) {
    ...
}

Browser other questions tagged

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