Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8398991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:10:56+00:00 2026-06-09T21:10:56+00:00

I’m working on an internal signup form for classes. I am working on a

  • 0

I’m working on an internal signup form for classes. I am working on a simple function that will check if the user has selected classes with conflicting schedules ( E.g. A class on from 1-2 p.m. as well as a class from 1-3 p.m. on the same day.)

Assuming that I’m using mysqli and fetching the classes the user has selected from a MySQL table with start and end datetime fields, what is the most efficient and/or effective way to compare the multiple classes in order to see if they conflict?

EDIT: Here’s a sample table:

====================================================
|  id  |        start        |         end         | 
====================================================
|  1   | 2012-10-01 08:00:00 | 2012-10-01 08:00:00 |
====================================================

Obviously, there will be other data in the table (Like title, description, etc.), but I think I probably only need the data above to do comparisons. I will have an array with the id of every classes the user has signed up for.

Sorry if this is a duplicate, I did search and did not see this asked before.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:10:58+00:00Added an answer on June 9, 2026 at 9:10 pm

    I’m not sure what your primary key is for this table. I’m fairly confident that mine is wrong for you, but it shouldn’t matter for the purpose of your question.

    -- student_id and class_id are foreign keys, not shown
    create table times (
      student_id integer not null,
      class_id integer not null,
      primary key (student_id, class_id), 
    
      start_time timestamp not null,
      end_time timestamp not null
    );
    

    The first student has an overlap between classes 2 and 3.

    insert into times values
    (1, 1, '2012-09-01 08:00', '2012-09-01 08:55'),
    (1, 2, '2012-09-01 10:00', '2012-09-01 11:55'),
    (1, 3, '2012-09-01 11:45', '2012-09-01 12:45');
    

    Second student, no overlaps.

    insert into times values
    (2, 1, '2012-09-01 08:00', '2012-09-01 08:55'),
    (2, 2, '2012-09-01 10:00', '2012-09-01 11:55');
    

    Third student has an overlap between classes 1 and 2.

    insert into times values
    (3, 1, '2012-09-01 08:00', '2012-09-01 10:00'),
    (3, 2, '2012-09-01 09:55', '2012-09-01 11:55'),
    (3, 3, '2012-09-01 12:00', '2012-09-01 12:55');
    

    If your platform conformed to SQL-92, you could use the OVERLAPS operator. (If any dbms supported assertions, you could declare the tables in such a way that it wouldn’t be possible to insert overlapping classes.)

    select t1.*
    from times t1
    inner join times t2 on t1.student_id = t2.student_id
           and (t1.class_id <> t2.class_id and t1.start_time <> t2.start_time and t1.end_time <> t2.end_time )
           and (t1.start_time, t1.end_time) overlaps (t2.start_time, t2.end_time);
    

    MySQL doesn’t seem to support that, so you might use the equivalent definition from the SQL standards committee.

    select t1.*
    from times t1
    inner join times t2 on t1.student_id = t2.student_id
           and (t1.class_id <> t2.class_id and t1.start_time <> t2.start_time and t1.end_time <> t2.end_time )
           and (
                 (t1.start_time > t2.start_time and not (t1.start_time >= t2.end_time and t1.end_time >= t2.end_time))
                 or
                 (t2.start_time > t1.start_time and not (t2.start_time >= t1.end_time and t2.end_time >= t1.end_time))
                 or
                 (t1.start_time = t2.start_time and (t1.end_time <> t2.end_time or t1.end_time = t2.end_time))
           );
    

    I’d expect the join conditions to give you good performance, as long as your tables are indexed carefully. In your case, you’d be selecting rows for a single student, which should be pretty selective. (I didn’t do that in my example code.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.