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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:10:32+00:00 2026-06-01T04:10:32+00:00

I have a scenario where a user submits a start and stop date for

  • 0

I have a scenario where a user submits a start and stop date for a reservation in a hotel. The database has different (daily) prices for periods of the year. Given the user’s date range I want to start by counting the number of days the user’s range has in each range from the database and then multiplying that by the daily price for that date range.

User’s dates:

  03 Jun 2012 - 03 Jul 2012

Relevant date ranges from the DB

Array
(
    [0] => stdClass Object
        (
            [date_from] => 2012-04-09
            [date_to] => 2012-06-04
            [price] => 44
        )

    [1] => stdClass Object
        (
            [date_from] => 2012-06-04
            [date_to] => 2012-07-02
            [price] => 52
        )

    [2] => stdClass Object
        (
            [date_from] => 2012-07-02
            [date_to] => 2012-07-16
            [price] => 61
        )

)

As you can see the reservation begins in the first range, spans the second range and ends in the third range.

foreach ($dates as $key => $d)
{
    $days = 0;

    $user_start = strtotime($range['start']);
    $user_stop = strtotime($range['stop']);
    $db_start = strtotime($d->date_from);
    $db_stop = strtotime($d->date_to);

    if( $user_start >= $db_start && $user_stop < $db_stop )
    {
        $start = $range['start'];
        $stop = $range['stop'];
    }

    elseif( $user_start <= $db_start && $user_stop > $db_start )
    {
        $start = $d->date_from;
        $stop = $d->date_to;
    }    

    elseif( $user_start <= $db_stop && $user_stop > $db_stop )
    {
        $start = $range['start'];
        $stop = $d->date_to;
    }

    $days = calculate_nbr_days($start, $stop);

    $price += $days * $d->price;
}

This code almost works, except for the fact it takes the end of the reservation ($stop) as being the last date of the DB range instead of the user’s range and I can’t figure out why!

  • 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-01T04:10:34+00:00Added an answer on June 1, 2026 at 4:10 am

    Have you considered doing it all in SQL? Something this should work:

    SELECT
      SUM(
        datediff(
          IF($dateTo>date_to,date_to,$dateTo),
          IF($dateFrom<date_from,date_from,$dateFrom)
        ) * price_per_day
      ) as total_cost
    FROM ranges
    WHERE '$dateFrom'<=date_to
      AND '$dateTo'>=date_from
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scenario where I have to check whether user has already opened
I have the following scenario: I have various user's data stored in my database.
I have a scenario where a user will have access to a one-time-url. When
I have a scenario where a user is emailing another user in an HTML
I will like to know: I have a scenario. If a user adds a
Consider following scenario: I have RESTful URL /articles that returns list of articles user
I have a problem regarding getting the path of a user control. The scenario
My app has a main login/password screen where user enters credentials and submits the
I have a scenario where i need to have a user checkout through PayPal
Simple scenario: I have a signup form, with user name, password, email address, may

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.