Page 1 of 1

Testing for intersection of two date/time ranges

Posted: Thu Nov 06, 2003 6:16 am
by dickey
I would like to test for appointment conflicts in an app I am working on.

How does this method sound.

I retrieve a start and finish date-time from mysql for each appointment.
I use mktime and obtain a time stamp for each start finish time.
I then create and arrays using

array1 = range(starttime 1, endtime 1)
array2 = range(starttime 2, endtime 2)

I then attempt to use $intersect = array_intersect (array1, array2)

If $intersect is empty no app conflict etc, however this method is probably extremely memory intensive and on my machine times out.

Is the problem that array_intersect requires strings and that range provides arrays of integers.

Maybe someone has a better method of comparison/intersection of two or more date ranges.

Any help appreciated.

Posted: Thu Nov 06, 2003 6:47 am
by JayBird
i have recently done a similar project, but i tested claseshes in my SQL query like this

Code: Select all

$startsql = " SELECT * as current_appointment FROM appointments WHERE ((appointment_start < '$start_time' AND appointment_end > '$start_time') OR (appointment_start < '$end_time' AND appointment_end > '$end_time'))";
if the query returns nothing, then this means that no clash occurs :)

Mark

Posted: Thu Nov 06, 2003 1:21 pm
by dickey
Thanks Mark,

Your solution works well.

I expected this task was better performed at the db end.

Again Thanks.

Andrew