Jump to content

Help with time-related MySQL query needed!


Recommended Posts

New to the forum - hoping someone here can shed some light on this..! I only dabble in MySQL and am stuck here.

 

I'm building a type of calendar which is to display a list of recurring daily events. Every day the events are identical, and top of the list is the next event. I've managed to achieve this with the following:

 

 

$currentTime = date("H:i:s", time());$getEvent = mysql_query("SELECT * FROM events WHERE eventTime >= '$currentTime' ORDER BY eventTime");while($row=mysql_fetch_array($getEvent)){        echo ''.$row["eventTime"].'  - '.$row["eventName"].'';}

 

 

My problem is that until the $currentTime reaches 00:00:00 the past events are not shown. Whereas I need them to be displayed at the bottom of the list once their time has passed, in a cyclical fashion.

 

For example, at 15:01:00 the event with a time of 15:00:00 should be bottom of the list ready for the following day, rather than being hidden from view altogether.

 

Hope this makes sense - many thanks in advance for your help!

Thanks for the reply - eventTime is a TIME field, yes - though I've managed to fix this... an easy solution I'd completely overlooked!

 

Posted below just in case anyone is wondering.

 

$currentTime = date("H:i:s", time());

$getEvent = mysql_query("SELECT * FROM events WHERE eventTime >= '$currentTime' ORDER BY eventTime");
while($row=mysql_fetch_array($getEvent))

{
        
echo ''.$row["eventTime"].'  - '.$row["eventName"].'';

}

$getPastEvent = mysql_query("SELECT * FROM events WHERE eventTime < '$currentTime' ORDER BY eventTime");
while($row2=mysql_fetch_array($getPastEvent))

{
        
echo ''.$row2["eventTime"].'  - '.$row2["eventName"].'';

}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.