Check if a date is earlier than another date

5

Good morning, I would like to know if there is a way to check if one date is earlier or higher than another, in my code there are 2 date type inputs, which send their values to the database, however a date will start something and the other end, I'd like to do a check before sending the bank that does not allow the first date to be greater than the second, can you do that?

FORM:

<tr>
    <th>Data de Inicio:</th>
    <td><input type="date" class="form-control input-sm" name="Data_Inicio" required><br></td>
</tr>  
<tr>
    <th>Data de Encerramento:</th>
    <td><input type="date" class="form-control input-sm" name="Data_fim" required><br></td>
</tr>

PHP:

$Data01 = explode("-", $Data_in);
$Data02 = explode("-", $Data_fim);
$Ndata_in = $Data01[0] . '/' . $Data01[1] . '/' . $Data01[2];
$Ndata_fim = $Data02[0] . '/' . $Data02[1] . '/' . $Data02[2];

$sql = 'INSERT INTO walldata VALUES ("' . $Ndata_in . '", "' . $Ndata_fim . '")';
    
asked by anonymous 08.06.2016 / 13:30

3 answers

5

The most correct way is:

$data1 = new DateTime( '2016-06-06' );
$data2 = new DateTime( '2016-06-15' );

echo ($data1 < $data2) ? "Está Ok!" : "Data de encerramento errada";

In query you can use only the string of the pure date even in this format 1996-12-10 (aaaa-mm-dd)

    
08.06.2016 / 14:29
4

Colleague @lvcs already gave a good example of using Datetime , but I could not fail to say that if the dates are strings in the format already used by MySQL, in AAAA-MM-DD format, it is unnecessary to instantiate two objects DateTime .

Much simpler and more efficient to do the comparison directly:

$data_menor = '2016-06-01';
$data_maior = '2016-02-31';

echo ( $data_menor  < $data_maior ) ? "Está Ok!" : "Data de encerramento errada";


If you want to flip automatically it's also simple:

$data_menor = '2016-06-01';
$data_maior = '2016-02-31';

if( $data_menor > $data_maior ) {
   $tmp = $data_maior ;
   $data_maior = $data_menor ;
   $data_menor = $tmp;
}

$sql = 'INSERT INTO walldata VALUES ("' . $data_menor . '", "' . $data_maior . '")';


PS: changing above variables is much faster than doing list($x,$y) = array($y,$x);

    
08.06.2016 / 15:49
1

"make a check before sending to the bank that does not allow the first date to be greater than the second"

You can do this in two different ways:

  • Back end - php
  • Front end - javascript
  • I usually use javascript to avoid code in php. That said, using JS, there are at least 2 possibilities for this task.

  • Using pure javascript
  • Using some of the various JS libraries. I use datepicker of jquery.
  • This application allows multiple settings of the date field quite simply.

    I have a code that I use in my applications. It is bi directional. In the following scenario there is a date to deliver products and a date to relaunch the party. Then the user chooses the expected date to receive the product. When that date is chosen, the date of the party can only be greater than or equal to the date of arrival. There is also the possibility of the user to choose the date of the party first. So the product's maximum delivery date can only be the date of the event.

    Hope it helps, try to fit your needs.

        //Validar datas:
    
        //Quando abre o datapicker seleciona a menor data para ser o dia de hoje now().
        var now = new Date(),
        minDateJS = now.toISOString().substring(0,10);
    
        //Seleciona a data de entrega do produto.
        //Aplica a data now (hoje) para ser a menor data selecionavel para a entrega do produto
        //Aplica a data de entrega do produto para ser a menor data possivel do evento  
        $('#dt_entregaProd').datepicker({
            dateFormat: 'yy-mm-dd',
            changeMonth: true,
            changeYear: true,
            minDate: minDateJS,
            inline: true,         
            onClose: function( selectedDate ) {//Passa a data selecionada neste objeto para ser a menor data possivel para realizar o evento
              $( "#dt_Event" ).datepicker( "option", "minDate", selectedDate );
            }
    
        });             
    
    
        //Seleciona a data do evento.
        //Evento bidirecional. Se a data do evento for selecionada primeiro, entã a data máxima de entrega do produto será limitada automaticamente
        $('#dt_Event').datepicker({
            dateFormat: 'yy-mm-dd',
            changeMonth: true,
            changeYear: true,
            minDate: minDateJS,
            inline: true, 
            onClose: function( selectedDate ) {
              $( "#dt_entregaProd" ).datepicker( "option", "maxDate", selectedDate );
            }        
         });       
    
        
    08.06.2016 / 15:33