Filter php mysql table by date

Asked

Viewed 1,414 times

2

I have a table on a php page that displays customers who have sent leads to the site. There is how I create an input in which the user puts a date and only appears the leads sent on this date?

<?php
header('Content-Type: text/html; charset=utf-8');

$host = "localhost";
$usuario = "";
$senha = "";
$bd = "";

$mysqli = new mysqli($host,$usuario,$senha, $bd);  /*Conecta no bando de dados MySql*/

$consulta_carro = "SELECT * FROM automovel";
$consulta_moto = "SELECT * FROM moto";
$consulta_casa = "SELECT * FROM casa";

$con_carro = $mysqli->query($consulta_carro) or die ($conexao->error);
$con_moto = $mysqli->query($consulta_moto) or die ($conexao->error);
$con_casa = $mysqli->query($consulta_casa) or die ($conexao->error);

$datahora = "d/m/Y - H:i:s";

?>


<html>
 <head>

 </head>
 <body>
    <div class="center">
    <h2>Automóvel</h2>
    <table class="tabela">
        <tr>
            <!--<td>Valor</td>-->
            <td class="linha-nome">Nome</td>
            <td class="linha-cidade">Cidade</td>
            <td class="linha-telefone">Telefone</td>
            <td class="linha-email">Email</td>
            <td>Data de envio</td>
        </tr>
        <?php while($dado = $con_carro->fetch_array()){ ?>
        <tr>            
            <!--<td><?php // echo $dado['valor'] ?></td>-->
            <td class="capitalize"><?php echo $dado["nome"] ?></td>
            <td class="capitalize"><?php echo $dado["cidade"] ?></td>
            <td class="capitalize"><?php echo $dado["telefone"] ?></td>
            <td class="capitalize"><?php echo $dado["email"] ?></td>
            <td class="capitalize"><?php echo date($datahora, strtotime($dado['data_cadastro'])) ?></td>
        </tr>
        <?php } ?>
    </table>    
    </div>
 </body>
</html

If you want to see the tables, it’s in this page

1 answer

2

What you need would be something similar to this, I believe:

$dataInput = '31/10/2016 10:20:00'; //equivalente -> $dataInput = $_POST['data_automovel'];

//ou somente a data
$dataInput = '31/10/2016'; //equivalente -> $dataInput = $_POST['data_automovel'];

 //ou somente dia, mes e horário...
$dataInput = '31/10 10:20:00'; //equivalente -> $dataInput = $_POST['data_automovel'];

//entre outros likes...

$consulta_carro = "SELECT * FROM automovel
where DATE_FORMAT(data_envio, '%d/%m/%Y %H:%i:%s') LIKE '%$dataInput%'"; 

Or more precisely only by date:

$dataInput = '31/10/2016'; //equivalente -> $dataInput = $_POST['data_automovel'];

$consulta_carro = "SELECT * FROM automovel
where DATE_FORMAT(data_envio, '%d/%m/%Y') = '$dataInput'"; 

Browser other questions tagged

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