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
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*/
);

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.