{"id":22,"date":"2008-04-09T16:15:22","date_gmt":"2008-04-09T21:15:22","guid":{"rendered":"http:\/\/osric.com\/chris\/accidental-developer\/?p=22"},"modified":"2008-04-10T08:48:17","modified_gmt":"2008-04-10T13:48:17","slug":"checking-for-date-time-conflicts","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2008\/04\/checking-for-date-time-conflicts\/","title":{"rendered":"Checking for date-time conflicts"},"content":{"rendered":"<p>It seems like I&#8217;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:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/osric.com\/chris\/accidental-developer\/wp-content\/uploads\/2008\/04\/conflicting-time-slots.png\" alt=\"Conflicting Time Slots\" \/><\/p>\n<p>There are 5 unique conflicts possible, and it seems like I&#8217;m always forgetting one.<\/p>\n<p><!--more--> Here&#8217;s a ColdFusion function that I&#8217;ve been using to check the database for conflicts:<\/p>\n<p align=\"left\">\n<pre><code>&lt;cffunction name=\"hasConflicts\" access=\"private\" output=\"false\" returntype=\"boolean\" hint=\"Determines whether the specified start and end times have any conflicts\"&gt;\r\n&lt;cfargument name=\"startTime\" type=\"date\" required=\"true\" hint=\"Start datetime timestamp\"&gt;\r\n&lt;cfargument name=\"endTime\" type=\"date\" required=\"true\" hint=\"End datetime timestamp\"&gt;\r\n\t<!--- Initialize local variables --->\r\n\t&lt;cfset var getConflicts = \"\"&gt;\r\n\t&lt;cfset var myResult = TRUE&gt;\r\n\t<!--- Run query --->\r\n\t&lt;cfquery name=\"getConflicts\" datasource=\"#THIS.ds#\"&gt;\r\n\t\tSELECT id\r\n\t\tFROM reservations\r\n\t\tWHERE (\r\n\t\t\t(\r\n\t\t\t\tstart_timestamp &gt;= &lt;cfqueryparam value=\"#ARGUMENTS.startTime#\" cfsqltype=\"cf_sql_timestamp\"&gt; AND \r\n\t\t\t\tend_timestamp &lt;= &lt;cfqueryparam value=\"#ARGUMENTS.endTime#\" cfsqltype=\"cf_sql_timestamp\"&gt; \r\n\t\t\t) OR ( \r\n\t\t\t\tstart_timestamp &lt;= &lt;cfqueryparam value=\"#ARGUMENTS.startTime#\" cfsqltype=\"cf_sql_timestamp\"&gt; AND \r\n\t\t\t\tend_timestamp &gt;= &lt;cfqueryparam value=\"#ARGUMENTS.endTime#\" cfsqltype=\"cf_sql_timestamp\"&gt;\r\n\t\t\t) OR ( \r\n\t\t\t\tstart_timestamp &lt; &lt;cfqueryparam value=\"#ARGUMENTS.endTime#\" cfsqltype=\"cf_sql_timestamp\"&gt; AND \r\n\t\t\t\tend_timestamp &gt;= &lt;cfqueryparam value=\"#ARGUMENTS.endTime#\" cfsqltype=\"cf_sql_timestamp\"&gt; \r\n\t\t\t) OR ( \r\n\t\t\t\tstart_timestamp &lt;= &lt;cfqueryparam value=\"#ARGUMENTS.startTime#\" cfsqltype=\"cf_sql_timestamp\"&gt; AND \r\n\t\t\t\tend_timestamp &gt; &lt;cfqueryparam value=\"#ARGUMENTS.startTime#\" cfsqltype=\"cf_sql_timestamp\"&gt;\r\n\t\t\t)\r\n\t\t)\r\n\t&lt;\/cfquery&gt;\r\n\t<!--- If there are no conflicting records, set result to FALSE --->\r\n\t&lt;cfif getConflictingReservations.recordCount EQ 0&gt;\r\n\t\t&lt;cfset myResult = FALSE&gt;\r\n\t&lt;\/cfif&gt;\r\n\t<!--- Return result (boolean) --->\r\n\t&lt;cfreturn myResult&gt;\r\n&lt;\/cffunction&gt;<\/code>\r\n<\/pre>\n<p>There might be a more elegant query, but this one works. I&#8217;m posting it here for my own future reference, and anyone else that might find it useful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It seems like I&#8217;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 &hellip; <a href=\"https:\/\/osric.com\/chris\/accidental-developer\/2008\/04\/checking-for-date-time-conflicts\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Checking for date-time conflicts<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[349,25,23,24],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-coldfusion","tag-coldfusion","tag-date","tag-sql","tag-timestamps"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":0,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}