Jump to content

Find overlapping dates


ZwoksKorbi

Recommended Posts

Hi Guys,

 

currently i'm programming a web-based Calendar System to manage the assignment of seminar rooms.

If someone is assigning a new appointment he can choose the start and the end-date/time.

I need to check if the choosen room is available at the given dates.

I've tried to build the query on my own but i faild. :-\

So may someone can help me?

 

Things i have to check:

           NS        NE        
           |        |
    OS--OE |        |               //OK
        OS-|-OE     |               //Not OK
           | OS--OE |               //Not OK
           |     OS-|-OE            //Not OK
           |        | OS--OE        //OK
        OS-|--------|-OE            //Not OK

 

Here's the structure of my appointments table

CREATE TABLE IF NOT EXISTS `appointments` (
  `ID` int(11) NOT NULL auto_increment,
  `room` smallint(6) NOT NULL,
  `creator` varchar(60) collate utf8_bin NOT NULL,
  `subject` varchar(128) collate utf8_bin NOT NULL,
  `description` text collate utf8_bin NOT NULL,
  `Start` datetime NOT NULL,
  `End` datetime NOT NULL,
  `wholeday` tinyint(1) NOT NULL,
  `moreday` tinyint(1) NOT NULL,
  `isPublic` tinyint(1) NOT NULL default '1' COMMENT 'not implemented yet',
  PRIMARY KEY  (`ID`),
  KEY `Start` (`Start`,`End`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=8 ;

 

I'm looking forward hearing from you.

 

Korbi

Link to comment
https://forums.phpfreaks.com/topic/114995-find-overlapping-dates/
Share on other sites

The latest try:

SELECT 
COUNT(*) /* if >0 => not Ok*/
FROM 
`appointments` 
WHERE
(	
(
(	/*NewStart between old-start and -end */
	'$neuStart' >= UNIX_TIMESTAMP(`Start`)
	AND
	'$neuStart' < UNIX_TIMESTAMP(`End`)
) 
OR
(	/* NewEnd between old-start and -end */
	'$neuEnd' <= UNIX_TIMESTAMP(`End`)
	AND
	'$neuEnd' > UNIX_TIMESTAMP(`Start`)
)
OR	
(	/* New Appointment is completely between old-start and -end*/
	'$neuStart' <= UNIX_TIMESTAMP(`Start`)
	AND
	'$neuEnd' >= UNIX_TIMESTAMP(`End`)
)
OR	
(	/* old appointment is completely between new-start and -end*/
	'$neuStart' >= UNIX_TIMESTAMP(`Start`)
	AND
	'$neuEnd' <= UNIX_TIMESTAMP(`End`)
)

)

AND
`room` = '$room' /*search appointments only in choosen room*/
);

If I were checking I'd look for any bookings that do overlap my required slot so my query would be the other way round

 

SELECT COUNT(*) 
FROM bookings
WHERE (`Start` < '$ne') AND (`End` > $ns)
AND room = '$room'

 

If count is 0, book it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.