Compare Date within Mysql

Asked

Viewed 8,654 times

1

I’m trying to select in the bank the date closest to the current date but I’m not getting a good result.

The insertion is:

$titulo = trim($_POST["titulo"]);
$descricao = trim($_POST["descricao"]);
$data = $_POST['data'];

the consultation is:

SELECT * FROM eventos WHERE data > GETDATE() LIMIT 1

What I need is to get the DATE column in the event table where the registered DATE is greater than the current date.

  • In the bank this query return some line?

2 answers

4


You can develop as follows:

$DataAtual = date("Y-m-d"); 
$sql = "SELECT * FROM tabela WHERE data > {$DataAtual}";

This way it looks for records that are larger than the current date.

Or if $data comes from the Post, do it:

$DataAtual = $_POST['data'];
$DataAtual = date("Y-m-d", strtotime($_POST['data'])); // caso a data venha em formato 00/00/0000 
$sql = "SELECT * FROM tabela WHERE data > {$DataAtual}";

Remember that the command data > {$DataAtual} only serves if the date is HIGHER than the posted date, if it is equal to or greater, should be used data >= {$DataAtual}.

This documentation can help you: http://www.w3schools.com/sql/sql_dates.asp

  • To register the date I use the same $_POST? because if it will not register with / or - and does not maintain a standard for comparison. If I use $data = date("d-m-Y", $_POST['data']); It will insert: 01-09-1970 If I use $data = date("DD-MM-YY", $_POST['data']); will insert: Tuetue-Sep-197019701970

  • Yes, you can treat her in return. I inserted there the type of example, so that you have an idea... just decide which format comes and delete the incorrect line.

  • Andre, I haven’t made it yet. I send via post and pick up with $data = date("d-m-y", $_POST['data]'); however the dates do not match when I enter into mysql, if I insert 210590 what appears in the database is: 03-01-70

  • $Current date = date("Y-m-d", strtotime($_POST['data'])); - Try this way.

  • also not working, even if I put a future date the code entered the date today.

  • What is the return?

  • @Sir, I have something similar, but it’s not the same, could you help me? https://answall.com/questions/383064/select-case-when-datediff-retornar-expirated-ou-n%C3%a3o-com-problema-na-sa%C3%Adda

Show 3 more comments

-2

I made a small adjustment to work on my system

$sql = "SELECT * FROM table WHERE data > '". $Current date."'";

but the logic is the same

Browser other questions tagged

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