Jump to content

Recommended Posts

I'm trying to display all events that are in a given month. The events have 'start' and 'end' datetimes in my db. I came up with this query but it only grabs events that start AND end on the same month. I'm trying to figure out how to query events that span multiple months. Ex: Start in the previous month and end in the the current month; Start in the current month and end in the next month, or start in the previous month and end after the current month. I'd appreciate the help, thanks.

 

<?php
$first = "2011-06-01";
$last = "2011-06-30";
$cal_id = "77";

$query = "SELECT * FROM Bookings WHERE DATE(START) AND DATE(END) BETWEEN  '$first' AND  '$last' AND cal_id =  '$cal_id'";
?>

 

try something like this: (I'm assuming your database fields are in the date format YYYY-MM-DD)

 

$yesterday = date("Y-m-d",strtotime("- 1 day");

 

$query = "select * from `Bookings` where (`startDate` BETWEEN  '$first' AND  '$last') and (`endDate` > '$yesterday') ";

 

// also assuming you do not want to grab expired records, thats why I added condition that end date should be greater than yesterday.

 

hope this helps

I may be wrong, but I am thinking something like this:

 

<?php
$first = "2011-06-01";
$last = "2011-06-30";
$cal_id = "77";

$query = "SELECT * FROM Bookings WHERE (startDate >= '$first' OR endDate <= '$last') AND cal_id =  '$cal_id'";
?>

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.