How to check if Data X is newer than Data Y?

Asked

Viewed 1,545 times

1

I have the following variables::
$dt_local: who’s saving comic book value;
$dt_remoto: which is holding value from an external source;
So far so great, I need to compare the two and keep the most current date in the BD, both variables are coming as String.

How can I resolve it more quickly and simply?

  • 1

    Related or even repeated: https://answall.com/questions/33469/como-comparar-datas-em-php

  • what is the date format? dd/mm/yyyy?

2 answers

2

Assuming you have these dates in some defined format, you can use the class DateTime of PHP:

$dt_local = DateTime::createFromFormat('d-m-Y', '03-04-2017');
$dt_remoto = DateTime::createFromFormat('d-m-Y', '01-04-2017');

if ($dt_local > $dt_remoto) {
    echo "Local é mais recente", PHP_EOL;
} else if ($dt_local < $dt_remoto) {
    echo "Remoto é mais recente", PHP_EOL;
} else {
    echo "Datas são iguais", PHP_EOL;
}

The exit would be: Local é mais recente.

Whether the date may be in any pattern defined in documentation PHP, use the class constructor itself:

$dt_local = new DateTime('03-04-2017');
$dt_remoto = new DateTime('01-04-2017');

if ($dt_local > $dt_remoto) {
    echo "Local é mais recente", PHP_EOL;
} else if ($dt_local < $dt_remoto) {
    echo "Remoto é mais recente", PHP_EOL;
} else {
    echo "Datas são iguais", PHP_EOL;
}

In theory, any format provided in the table below will be accepted by the application.

inserir a descrição da imagem aqui

  • but how to play the dates coming from post within the Datetime function?

  • @Leocaracciolo something like $dt_local = new DateTime($_POST['dt_local']).

  • @Leocaracciolo What has happened?

  • did not read the localized notations and was entering the dates in the wrong format and then was giving error.

0

One way to check is by using the function max ( $valor1 , $valor2 ) (PHP 4, PHP 5, PHP 7)

where $value1 and $value2 are compatible values in dd/mm/yyyy or dd-mm-yyyy format.

Handbook

<?php
function inverteData($data){
    if(count(explode("/",$data)) > 1){
        return implode("-",array_reverse(explode("/",$data)));
    }elseif(count(explode("-",$data)) > 1){
        return implode("/",array_reverse(explode("-",$data)));
    }
}

if  (($_POST["dt_local"])&&($_POST["dt_remoto"])){

    $dt_local=inverteData($_POST["dt_local"]);  

    $dt_remoto=inverteData($_POST["dt_remoto"]);

    if ($dt_local!=$dt_remoto){
       $maior = max(array($dt_local,$dt_remoto));
       echo inverteData($maior);
    }else{
       echo "as datas são iguais";;
    } 
}
?>
  • Just one detail: using max, the date returned on $maior would be 05-04-2017, as it concerns string, is compared alphabetically character to character. In this case, being the first character equal, when checking the second, PHP considers 5 greater than 3, returning the first date as higher, ie, not even checking the rest of the string. For this method to work well, the date should be in the format Y-m-d, so he would check the month before checking the day, not considering the day 05/04 higher than 03/05.

  • 1

    ready I edited the answer, two heads think better than one right no?

Browser other questions tagged

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