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

  • Home
  • SEARCH
  • 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 8613715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:57:02+00:00 2026-06-12T04:57:02+00:00

I’m making a booking system in PHP/MySQL. I want to be able to select

  • 0

I’m making a booking system in PHP/MySQL.

I want to be able to select a month, select a booking length, and find how many periods of the given length are available (ie, not marked as booked) within that month.

I started by creating a table of days, each day has a field that indicates if it’s reserved or not, as well as a date. The two tables are:

CREATE TABLE `day` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `date` date NOT NULL,
  `booking` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) 

CREATE TABLE `booking` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `start` date DEFAULT NULL,
  `end` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) 

So far I’ve been able to count how many days in a month aren’t booked – but that doesn’t necessarily tell me if say there are 7 days in a row:

SELECT  
    EXTRACT(YEAR FROM date) as year,
    EXTRACT( MONTH FROM date) as month,
    count(day.id)
FROM
    day
WHERE booking IS NULL
GROUP BY year, month

At this point I’m considering loading all the days for my given month, and iterating through them in PHP to make up my numbers, but it seems kinda dumb and inefficient. Does anyone have a suggestion how I can find this information directly from MySQL?

  • 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-12T04:57:03+00:00Added an answer on June 12, 2026 at 4:57 am

    Assuming you want ranges of at least 7 days in July, 2012, then:

    SELECT
      DATE_FORMAT(start, '%Y-%m-%d') start,
      MAX(days) AS days,
      DATE_FORMAT(start + INTERVAL MAX(days) - 1 DAY, '%Y-%m-%d') end
    FROM (  
      SELECT
        d2.`date` start,
        (@days := IF(IFNULL(d2.booking, 0) = 0, @days + 1, 1)) AS days
      FROM
        (SELECT @days := 1) a,
        day d1
      LEFT JOIN  
        day d2 on d2.`date` = d1.`date` + INTERVAL 1 DAY
      WHERE
        IFNULL(d1.booking, 0) = 0
    ) d1
    GROUP BY
      start
    HAVING
      (
        EXTRACT(YEAR_MONTH FROM start) = 201207 OR
        EXTRACT(YEAR_MONTH FROM (start + INTERVAL MAX(days) - 1 DAY)) = 201207
      ) AND
      days >= 7
    ORDER BY
      start
    

    will produce this output:

    |      START | DAYS |        END |
    ----------------------------------
    | 2012-06-25 |   13 | 2012-07-07 |
    | 2012-06-26 |   12 | 2012-07-07 |
    | 2012-06-27 |   11 | 2012-07-07 |
    | 2012-06-28 |   10 | 2012-07-07 |
    | 2012-06-29 |    9 | 2012-07-07 |
    | 2012-06-30 |    8 | 2012-07-07 |
    | 2012-07-01 |    7 | 2012-07-07 |
    | 2012-07-23 |    8 | 2012-07-30 |
    | 2012-07-24 |    7 | 2012-07-30 |
    

    The clause

    (  
        EXTRACT(YEAR_MONTH FROM start) = 201207 OR
        EXTRACT(YEAR_MONTH FROM (start + INTERVAL MAX(days) - 1 DAY)) = 201207
    ) 
    

    is used to include all ranges, even if the range extends into the previous, or next month. If you want ranges that fall exclusively in a single month, use:

    EXTRACT(YEAR_MONTH FROM start) = 201207 AND
    EXTRACT(YEAR_MONTH FROM (start + INTERVAL MAX(days) - 1 DAY)) = 201207
    

    See http://sqlfiddle.com/#!2/4e36a/4 for an interactive demo.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I want to show the soap response to UIWebview.. my soap response is, <p><img

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.