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 6955931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:48:05+00:00 2026-05-27T14:48:05+00:00

I have 3 columns: Day, start_schedule and end_schedule. Day | start_schedule | end_schedule ——-|—————–|————–

  • 0

I have 3 columns: Day, start_schedule and end_schedule.

   Day  |  start_schedule | end_schedule
 -------|-----------------|--------------
 Monday |     1:00        |   3:00
 Monday |     6:00        |   8:00
 Monday |     8:00        |  10:00

I’m still learning php. How will I filter if my input start schedule and end schedule is valid based on the stored times in the database?

For example if I want to add times

  • start_schedule = 3:00 and end_schedule = 7:00

since there is 6:00 - 8:00 you shouldn’t be allowed to add this schedule.

  • start_schedule = 7:00 and end_schedule = 11:00

since there are 6:00 - 8:00 and 8:00 - 10:00 you shouldn’t be allowed to add this schedule.

  • 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-05-27T14:48:06+00:00Added an answer on May 27, 2026 at 2:48 pm

    I would do most of the logic in mysql since it will handle these kinds of expressions very easy and straight forward approaches.

    In PHP, before inserting a new entry we will need to check 1 thing:

    1. Is end_time less than start_time? If so, abort.

    In MySQL, when trying to insert a new entry we will need to check 4 things:

    1. Is the start_time inside any existent entry? If so, abort.
    2. Is the end_time inside any existent entry? If so, abort.
    3. Is there any entry starting that would be inside our new entry? If so, abort.
    4. Is there any entry ending that would be inside our new entry? If so, abort.

    The above can actually be refactored into one steps:

    1. Is there our start time less than any entries end time, AND is our end time larger than that entires start time? If so, abort.

    mysql> describe sample;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | start | time        | YES  |     | NULL    |       |
    | end   | time        | YES  |     | NULL    |       |
    | day   | varchar(50) | YES  |     | Monday  |       |
    +-------+-------------+------+-----+---------+-------+
    
    mysql> SELECT * FROM sample;
    +--------+----------+----------+
    | day    | start    | end      |
    +--------+----------+----------+
    | Monday | 01:00:00 | 03:00:00 |
    | Monday | 06:00:00 | 08:00:00 |
    | Monday | 08:00:00 | 11:00:00 |
    +--------+----------+----------+
    

    Sample implementation in PHP querying the database:

    Props to @regilero for posting a re-factored version of the where claus required.

    $new_entries = array (
      array('Monday', '00:00:00', '23:59:00'),
      array('Monday', '12:00:00', '15:00:00'),
      array('Monday', '07:00:00', '10:00:00')
    );
    
    foreach ($new_entries as $new) {
      list ($day, $start, $end) = $new;
    
      $q_day   = "'$day'";
      $q_end   = "'$end'";
      $q_start = "'$start'";
    
      $sql = <<<EOQ
        INSERT INTO `sample` (day,start,end)
        SELECT $q_day, $q_start, $q_end FROM DUAL
        WHERE NOT EXISTS (
          SELECT * FROM `sample`
          WHERE day = $q_day AND (
            $q_start < end AND $q_end > start
          )
        )
    EOQ;
    
      mysql_query ($sql) or die (mysql_error ());
    
      if (mysql_affected_rows () == 0)
        print join (' ', $new) . " was not inserted!\n";
      else
        print join (' ', $new) . " was inserted!\n";
    }
    

    This would output:

    Monday 00:00:00 23:59:00 was not inserted!
    Monday 12:00:00 15:00:00 was inserted!
    Monday 07:00:00 10:00:00 was not inserted!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three columns, y, m, and d (year, month, and day) and want
I have a table called Sessions with two datetime columns: start and end. For
I have a table with a number of columns that will be used to
I have the following query: SELECT location, step, COUNT(*), AVG(foo), YEAR(start), MONTH(start), DAY(start) FROM
I have a system that receives input from the public each day. Each morning
I have the following 6 integer db columns: Year Day Month Hour Minute Second
I'm running Oracle 10g and have columns with Type_Name TIMESTAMP(6) WITH TIME ZONE When
I have 62 columns in a table under SQL 2005 and LINQ to SQL
I have three columns in my table - company_name, first_name, last_name . In the
I have 12 columns separated by a tab. How can I join them side-by-side?

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.