How To Find Time Conflicts

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
pmcnally
Forum Newbie
Posts: 2
Joined: Wed Nov 03, 2004 3:08 pm

How To Find Time Conflicts

Post by pmcnally »

I am working on a class scheduler, and I am having trouble figuring out how to solve the problems of deciding if a class is conflicting with one another.

Say I have 2 classes
Class 1 runs from 14:00 to 15:15
Class 2 runs from 15:00 to 15:50

Those class times are definately overlapping. Can anybody help me figure out how to tell if they are over lapping?

Here's another set of test data that is eluding me:
Class 1 runs from 18:00 to 21:00
Class 2 runs from 19:00 to 19:50

If anybody could help me out, that would be really great and much appreciated =)

-Pat
swdev
Forum Commoner
Posts: 59
Joined: Mon Oct 25, 2004 8:04 am

Post by swdev »

A simpe test would be

Code: Select all

if (Class1EndTime > Class2StartTime)
{
  you have a clash
}
Hope this helps
pmcnally
Forum Newbie
Posts: 2
Joined: Wed Nov 03, 2004 3:08 pm

Post by pmcnally »

That's too simple because all the classes that started or ended after the first class would show up.
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

overlap if:

class2.start <= class1.end and class2.end >= class1.start
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: How To Find Time Conflicts

Post by Weirdan »

pmcnally wrote:I am working on a class scheduler, and I am having trouble figuring out how to solve the problems of deciding if a class is conflicting with one another.
The solution depends on two things:
  • How do you store the data (database, php arrays, something else)?
  • Do you need to check for overlapping for just one class (i.e. when class is added you run the check if it would fit in schedule) or you would got entire schedule somehow and then check if it has any overlapping classes?
Post Reply