Checking for date-time conflicts
It seems like I’ve worked on a lot of applications lately that involve time slots: room reservation systems, appointment schedulers, and so on. One thing that tripped me (and some other developers) up at first was checking for conflicting time slots. It turns out, there are a variety of possible conflicts:

There are 5 unique conflicts possible, and it seems like I’m always forgetting one.
Here’s a ColdFusion function that I’ve been using to check the database for conflicts:
<cffunction name="hasConflicts" access="private" output="false" returntype="boolean" hint="Determines whether the specified start and end times have any conflicts">
<cfargument name="startTime" type="date" required="true" hint="Start datetime timestamp">
<cfargument name="endTime" type="date" required="true" hint="End datetime timestamp">
<cfset var getConflicts = "">
<cfset var myResult = TRUE>
<cfquery name="getConflicts" datasource="#THIS.ds#">
SELECT id
FROM reservations
WHERE (
(
start_timestamp >= <cfqueryparam value="#ARGUMENTS.startTime#" cfsqltype="cf_sql_timestamp"> AND
end_timestamp <= <cfqueryparam value="#ARGUMENTS.endTime#" cfsqltype="cf_sql_timestamp">
) OR (
start_timestamp <= <cfqueryparam value="#ARGUMENTS.startTime#" cfsqltype="cf_sql_timestamp"> AND
end_timestamp >= <cfqueryparam value="#ARGUMENTS.endTime#" cfsqltype="cf_sql_timestamp">
) OR (
start_timestamp < <cfqueryparam value="#ARGUMENTS.endTime#" cfsqltype="cf_sql_timestamp"> AND
end_timestamp >= <cfqueryparam value="#ARGUMENTS.endTime#" cfsqltype="cf_sql_timestamp">
) OR (
start_timestamp <= <cfqueryparam value="#ARGUMENTS.startTime#" cfsqltype="cf_sql_timestamp"> AND
end_timestamp > <cfqueryparam value="#ARGUMENTS.startTime#" cfsqltype="cf_sql_timestamp">
)
)
</cfquery>
<cfif getConflictingReservations.recordCount EQ 0>
<cfset myResult = FALSE>
</cfif>
<cfreturn myResult>
</cffunction>
There might be a more elegant query, but this one works. I’m posting it here for my own future reference, and anyone else that might find it useful.
5 Comments to Checking for date-time conflicts
Leave a comment
Pages
Archives
- April 2012
- March 2012
- January 2012
- December 2011
- November 2011
- September 2011
- August 2011
- July 2011
- June 2011
- May 2011
- March 2011
- February 2011
- January 2011
- November 2010
- May 2010
- March 2010
- January 2010
- December 2009
- October 2009
- September 2009
- August 2009
- July 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
I must change the formatting for code. This looks ugly!
See, I have been having the same problem.
If you throw a
hrmm. 20 seconds on google: http://lorelle.wordpress.com/2007/02/23/wordpress-plugins-that-help-you-write-code/
I installed the plugin. If you wrap it in a <pre> block and then a <code> block it looks better, although the lines are too long for this puny 450px width. Looks like it’s time to update the site styles as well and give ourselves a little elbow room.
great article. I will bookmark this