Querying a date between two dates [duplicate]

0

You needed to check if a particular date is contained within a date range. Being more specific, in a system that we have control of vehicles, when we receive a fine, we need to identify the driver who was with a particular vehicle on the day in question.

Example:

$data_saida (registrada quando o veiculo sai)
$data_retorno (registrada quando o veiculo retorna)

$data_infracao = (registrada quando chega a multa)
$placa_veiculo = (registrada quando chega a multa)

Which method do I use to confirm according to the time of the infraction, who was with the car by the date, since we have several drivers trained? The vehicle was easy because the card comes in fine and is a unique identifier ...

Here is the code I'm running:

<?php
$data = date('Y-m-d');
$hora = date("H:i:s");

$placa = $_POST ['multas'];
$data_i = $_POST ['data_i'];
$hora_i = $_POST ['hora_i'];

$sql = "SELECT * FROM trafego WHERE '$data_i' BETWEEN 'data_s' AND 'data_e' AND placa='$placa'";
$verifica = mysqli_query ($strcon,$sql) or die (mysqli_error());
$row = mysqli_num_rows ($verifica);
    if ($row >=1) {
while ($resultado = mysqli_fetch_array($verifica)){
    $id_trafego = $resultado ['id_trafego'];
    $condutor = $resultado ['condutor'];
    $data_s = $resultado ['data_s'];  
    $hora_s = $resultado ['hora_s'];
    $data_e = $resultado ['data_e'];
    $hora_e = $resultado ['hora_e'];

    echo "<hr/>";
    echo $id_trafego;
    echo "<br/>";
    echo "CONDUTOR:&nbsp;&nbsp;",$condutor;
    echo "<br/>";
    echo "DATA SAÍDA:&nbsp;&nbsp;",date('d/m/Y',strtotime($data_s)),"&nbsp;&nbsp;&nbsp;","HORA DA SAÍDA:&nbsp;&nbsp;",$hora_s;
    echo "<br/>";
    echo "DATA RETORNO:&nbsp;&nbsp;",date('d/m/Y',strtotime($data_e)),"&nbsp;&nbsp;&nbsp;","HORA RETORNO:&nbsp;&nbsp;",$hora_e;
    echo "<hr/>";
    echo "<br/>";
    echo "<br/>";


}
    }else{
        echo "<p><h3>Não existe registro de veículos em tráfego na data da multa.<br/>";
        echo "Verifique a data digitada, e tente novamente</p></h2>";
    }
?>
    
asked by anonymous 24.04.2018 / 03:15

1 answer

0

I wanted to thank everyone for their help.

I made a change to the code by placing the second condition WHERE in the 'board' case, before BETWEEN and worked correctly. I'll paste the changed code below.

The variable $data_i (date of the infraction) comes next, but in this case now after the 'plaque'. previously the 'board' condition was at the end.

FIXED CODE

$sql = "SELECT * FROM trafego WHERE placa='$placa'AND'$data_i' BETWEEN data_s AND data_e";
$verifica = mysqli_query ($strcon,$sql) or die (mysqli_error());
$row = mysqli_num_rows ($verifica);
        if ($row >=1) {
while ($resultado = mysqli_fetch_array($verifica)){
    $id_trafego = $resultado ['id_trafego'];
    $condutor = $resultado ['condutor'];
    $data_s = $resultado ['data_s'];  
    $hora_s = $resultado ['hora_s'];
    $data_e = $resultado ['data_e'];
    $hora_e = $resultado ['hora_e'];
}
    
26.04.2018 / 01:02