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) {
...
}
Use the class
DateTime
. Compare string will only work if it is in formatY-m-d H:i:s
– Woss
Sorry I asked, but I can use these strings to transform into Datetime ?
– Elizandro Schmidt