Date Comparison - PHP [duplicate]

2

I have the following comparison:

# Verifica se está em tempo habil para concluir a transacao
if($transacao->data_expira <= date("Y-m-d H:i:s")." 000000"){
    echo "Ainda é permitido pagar"; 
} else {
    echo "Esta cobrança não pode ser mais paga!";
}
  

Where $ transcao- > data_expira is = 2017-10-05 15: 42: 54.000000

Data Atual: 2017-10-04 19:14:20
Data Expira: 2017-10-05 15:42:54

In this case, payment would have to be allowed. How can I make this comparison correctly?

    
asked by anonymous 04.10.2017 / 19:11

2 answers

2

Given that $transacao->data_expira is of type DateTime , you can compare using the ->diff() function, see:

if ($transacao->data_expira->diff(new DateTime("today")) >= 0) {
    echo "Ainda é permitido pagar";
} else {
    echo "Esta cobrança não pode ser mais paga!";
}

If $transacao->data_expira is not of type DateTime , start a new instance and then compare them.

$data_expira = new DateTime($transcao->data_expira); // 2017-10-05 15:42:54
$hoje = new DateTime("today"); // 2017-10-04
$intervalo = $data_expira->diff($hoje);
echo $interval->format("%a days"); // 1 days

See working in ideone

    
04.10.2017 / 19:20
1

You can compare the timestamp of dates like this:

if (strtotime($numerical." ".$day." of ".date("F")) < time()) {
    // Mais velho
} else {
    // Mais novo
}

Before converting the date to str by breaking it.

Timestamp

A time stamp (or time stamp) is a string denoting the time or date that a certain event occurred. The string is usually presented in a consistent format, allowing easy comparison between two distinct time stamps.

They are standardized by the International Organization for Standardization (ISO) through ISO 8601.

Source: link

    
04.10.2017 / 19:17