Jump to content

MySQL Near date range


bigheadedd
Go to solution Solved by Barand,

Recommended Posts

Hi,

 

I've been trying to sort out some issues I've been having with a query to fetch dates from an events based web app I've done.

Currently, the query looks like this..

$listresult = mysql_query("SELECT * FROM events WHERE  (startdate >= NOW() OR enddate >= NOW()) ORDER BY events.startdate ASC LIMIT $amount")or die(mysql_error());

This works as it should do, by picking up events that are currently on. However, if there is an event that starts from 1st January 2013 and doesn't end until 1st October 2014 (Next year!), it places itself at the beginning of the list regardless.

 

What i'm trying to achieve is where the query orders them by the NOW() time, by bringing the nearest (starting or ending) to the top of the list, and the others further down.

 

Is this possible at all? I've been trying to rack my brains, but nothing has come up so far.

 

Any help would be great.

Thanks,

E

 

Link to comment
Share on other sites

  • Solution

 

 

This works as it should do, by picking up events that are currently on.

 

I am surprised at that

 

 

 

WHERE (startdate >= NOW()

 

that will pick up future events. For current events you need

 

WHERE startdate <= NOW() AND enddate >= NOW()

 

Perhaps you need to sort by enddate

Link to comment
Share on other sites

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.