Search next date in database

Asked

Viewed 71 times

0

I am creating a mysql table containing the date of an event. The question is as follows: if on the present day there is an event it is highlighted otherwise indicates the day of the next event. would they have any idea how to do it? The query for the current day would be more like searching for the following if it does not have the current day.

$data_atual = date('Y-m-d');
$resultado_evento = mysql_query("SELECT * FROM agenda WHERE data like '%$data_atual%'");
while($linhas_evento = mysql_fetch_array($resultado_evento)) {
    echo "nome " . $linhas_evento['nome'] . "<br>";
}

1 answer

1


Use greater equal and set a limit to 1, if you have an equal date today, show it, or show the next date. Don’t forget to give an ORDER BY by the date.

$data_atual = date('Y-m-d h:i:s');
$resultado_evento = mysql_query("SELECT * FROM agenda WHERE data >= '$data_atual' ORDER BY data ASC LIMIT 1");
  • vlw fall by the help, I’m starting php so sometimes I get lost with these selects but it worked right. hugs

  • Legal @Alexandre mark as the right answer here!

  • At first I thought it worked out but do not know the pq, I’m using datatime more type have a first field 2016-09-01 14:00 and a second 2016-09-01 16:00 more passing the time of 14 hrs or more he continues reading the first field and does not pass to the following which would be the 16 hrs tenntei use the condition if more still continues reading only the first field.

  • Something else is wrong with my code, not the call, I isolated it on another page and it worked correctly. I’ll check if I didn’t duplicate some variable that conflicted

  • @Alexandre in $data_actual you need to put the time then, this check I did was only with the day. I edited the answer

  • in select I left an Hour(date) >= $hours so it selects only from the current time and if in the next event of the following day it should be presented if there was no other event on the current day and the time is less than in select it is cancelled until the next day. would be able to leave Hour(date) > 00:00:00 ? and what would be the syntax used in the case?

Show 1 more comment

Browser other questions tagged

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