Comparison of dates in PHP

Asked

Viewed 246 times

-1

I’m doing this code to compare the current date with the date I entered into the form, I used this logic to do this, but apparently it’s not working. What could be wrong?

<?php 
$data = $_POST['data'];

$nData = getdate();

$dataAno = date('y',strtotime($data));
$dataMes = date('m',strtotime($data));
$dataDia = date('d',strtotime($data));


if($dataAno > $nData['year']){
    echo "Essa data não passou ainda..";

}elseif ($dataAno == $nData['year']){

    if($dataMes > $nData['mon']){
        echo "Essa data não passou ainda..";

    }elseif($dataMes == $nData['mon']){

        if($dataDia > $nData['mday']){
            echo "Essa data não passou ainda...";

        }elseif($dataDia == $nData['mday']){

            echo "Essa data é a data de hoje!!!";
        }echo "Essa data ja passou";
    }

}

?>

The form looks like this:

<html>
<form method="post" action="teste.php">
    <label>Data:</label>
    <input type="date" name="data"><br><br>


    <button type="submit">Enviar</button>
</form>

  • and how the date of the form is coming?

  • your script has only one error $dataAno = date('y',strtotime($data)); with y minusculo. solution see my answer.

3 answers

2

That should work already

$dataInformada = '2018-04-13';
$dataAtual = date( 'Y-m-d' );
if( $dataInformada < $dataAtual ) {
    echo 'Data informada menor que data atual';
}

1

You can turn the date directly into an array with the explode function:

<?php 

if(isset( $_POST['data'])){

    $data = explode('-' , $_POST['data']);

    $nData = getdate();

    $dataAno = $data[0];
    $dataMes = $data[1];
    $dataDia =$data[2];


    if($dataAno > $nData['year']){
        echo "Essa data não passou ainda..";

    }elseif ($dataAno == $nData['year']){

        if($dataMes > $nData['mon']){
            echo "Essa data não passou ainda..";

        }elseif($dataMes == $nData['mon']){

            if($dataDia > $nData['mday']){
                echo "Essa data não passou ainda...";

            }elseif($dataDia == $nData['mday']){
                echo "Essa data é a data de hoje!!!";
            }
        }

    }

}
?>
<form method="post" >
    <label>Data:</label>
    <input type="date" name="data"><br><br>   
    <button type="submit">Enviar</button>
</form>

There are more efficient and more economical ways to compare dates with PHP. But with this you can build your algorithm. I think that’s the proposal.

  • Still doesn’t print anything on the screen

  • I edited the question and put the full code. Test and see if it works.

  • It worked, but then in case just needed the if(isset)?

  • I didn’t want to interfere with your algorithm to verify the values. I believe your goal is to create it yourself.

  • @Mboss isset checks if the post variable is with content other than NULL. If it returns true the code within the if block is executed.

1


One of the errors in your code is:

In the line below brings you the year with two digits, ie, 18

$dataAno = date('y',strtotime($data));

and you’re comparing it to $nData[year] returning 2018

So change the y minuscule by Y uppercase to return the year with 4 digits

$dataAno = date('Y',strtotime($data)); // 2018

The comparisons of months and days must also have their format changed in order to be able to compare correctly (will that along the code make a comparison of type strlen :).

$nData['mon'] //Representação numérica de um mês 1 a 12

$nData['mday'] // Representação numérica do dia do mês 1 a 31

so, to compare with the above values, we must have

// n - representação numérica de um mês, sem zero à esquerda
$dataMes = date('n',strtotime($data));

// j - dia do mês sem zero à esquerda
$dataDia = date('j',strtotime($data));

Plus, their if else if in certain situations

Corrected code

<?php 
if(isset( $_POST['data'])){

    $data = $_POST['data'];

    $nData = getDate();

    $dataAno = date('Y',strtotime($data));
    $dataMes = date('n',strtotime($data));
    $dataDia = date('j',strtotime($data));


    if($dataAno > $nData['year']){
        echo "Essa data não passou ainda..";

    }elseif ($dataAno == $nData['year']){

        if($dataMes > $nData['mon']){
            echo "Essa data não passou ainda..";

        }elseif($dataMes == $nData['mon']){

            if($dataDia > $nData['mday']){
                echo "Essa data não passou ainda...";
            }elseif($dataDia == $nData['mday']){
                echo "Essa data é a data de hoje!!!";
            }else{
                echo "Essa data ja passou";
            }
        }

    }else{
       echo "Essa data ja passou";
    }
}

?>
  • Worked too!

Browser other questions tagged

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