Jump to content

between date help


dadamssg

Recommended Posts

I'm have events stored in my database with start and stop datetimes. I am trying to write a sql query where i pull events occur on a particular day. The event could start on that day, end on that day, OR start before the day and end after the day. Any help would be much appreciated!

Link to comment
Share on other sites

i'm sure BETWEEN would work. i don't know the correct syntax to use though. I'm now familiar with querying with dates.

 

i have something like the following but its not workin...


$date = "2010-11-25";

$query = "SELECT * FROM `Events` WHERE date('2010-11-08') BETWEEN start AND end";


Link to comment
Share on other sites

i'm sure BETWEEN would work. i don't know the correct syntax to use though. I'm now familiar with querying with dates.

 

i have something like the following but its not workin...


$date = "2010-11-25";

$query = "SELECT * FROM `Events` WHERE date('2010-11-08') BETWEEN start AND end";


 

That looks good (if start and end are column names in your table and are defined as DATE or DATETIME). Note that the $date (PHP) variable is not being used and is not the same value as you put in the query.

 

You could add code to print mysql_error() if the query fails so you can see why

 

$date = "2010-11-25";
$query = "SELECT * FROM `Events` WHERE date('$date') BETWEEN start AND end";
$result = mysql_query($query);
if ($result === false) {
  printf("Query Failed:\n%s\nError: %s\n", $query, mysql_error());
} else {
  // Looks Good, move on

 

 

Link to comment
Share on other sites

yes they are fields in my database that are datetimes. The query won't return events that start and end on the same day though, which is what i also want. So this query isn't what i'm looking for...

 

$date = "'2010-11-03 00:00:00";

$query = "SELECT * FROM Events WHERE DATE('$date') BETWEEN start AND end ORDER BY start ASC";


Link to comment
Share on other sites

BETWEEN is inclusive.

 

Is there some reason you have the time included as part of the start/end values?

 

Assuming the $date value is just a date -

 

SELECT * FROM Events WHERE '$date' BETWEEN date(start) AND date(end)

 

^^^ If that doesn't work, I recommend that you post a sample of your data, a $date value, and what the expected results should be.

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.