Select records smaller than the current date and time

4

What is the best way to query the database where the data is smaller than the current date and time

SELECT * FROM agenda WHERE agendamento < '".date('Y-m-d H:m:s')."'
    
asked by anonymous 03.08.2017 / 16:07

2 answers

5

You can use the MySQL function itself.

If your column has time:

SELECT *
  FROM agenda
 WHERE agendamento < NOW()
  

NOW

     

Returns the current date and time as a value in 'YYYY-MM-DD HH: MM: SS' or 'YYYYMMDDHHMMSS' format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone.

Free translation:

  

Returns the current date and time as the value in the format 'YYYY-MM-DD HH: MM: SS' or YYYYMMDDHHMMSS, depending on the context in which the function is used (string or numeric). The value is expressed in time zone current.

You can test here!

If your column is just a date:

SELECT *
  FROM agenda
 WHERE agendamento < CURDATE()
  

CURDATE

     

Returns the current date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the function is used in a string or numerical context.

Free translation:

  

Returns the current date as the value in the format 'YYYY-MM-DD' or YYYYMMDD, depending on the context in which the function is used (string or numeric).

    
03.08.2017 / 16:09
-2
$today = date("d/m/Y");
$dia = date("d");
$mes = date("m");
$ano = date("Y");
$total = 0;

function selectAllPessoa(){
    $banco = abrirBanco(); 
    $sql = "SELECT * FROM agenda WHERE status = 'N' AND(DAY(data_consulta)='$dia') AND (MONTH(data_consulta)) ORDER BY nome";

I have done this for years but in my code stopped fncionar try there1

    
11.07.2018 / 04:04