Testing for intersection of two date/time ranges

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dickey
Forum Commoner
Posts: 50
Joined: Thu May 16, 2002 8:04 pm
Location: Sydney, Australia

Testing for intersection of two date/time ranges

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
dickey
Forum Commoner
Posts: 50
Joined: Thu May 16, 2002 8:04 pm
Location: Sydney, Australia

Post by dickey »

Thanks Mark,

Your solution works well.

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

Again Thanks.

Andrew
Post Reply