Jump to content

spliting date range into months


rvdb86

Recommended Posts

Hi, I am developing a booking system, the start date and end date of the bookings are stored in the database.

Here's the problem: the price of the booking depends on the month of the booking e.g. December the price is hire. However, a user can start the booking in november and end in december, in this case the price is normal for november and higher for december.

 

If I have a start date of 15-11-2009 and end date of 23-12-2009, what would be the best way to work out the number of days in november and how many days in december?

 

Thanks in advanced for any suggestions!

Link to comment
Share on other sites

When given the year, if you've checked and know that the booking spans more than one moth, I would do something like this:

 

$end_of_month = mktime(11, 59, 59, $month, $last_day_in_month, $year); // get the unix timestamp for the end of the month
$start_of_booking_in_month = mktime(0, 0, 0, $month, $day_booking_starts, $year); // get the unix timestamp for the start of booking
$time_in_first_month = $end_of_month - $start_of_booking_in_month; // get the seconds between the end of the month and the start of booking
$days_in_first_month = ceil($time_in_first_month / 60 / 60 / 24); // convert the seconds to days rounding fractions up

 

With a little innovation you can figure out how to do this over multiple months using a loop. To get you started, you'll want to do something like this:

 

$what_year = $year;
$next_month = $month; // You could set this to the previous month (checking to see if you need to subtract a year) which would allow you to process
                     //  the entire booking inside of the loop below, just remember that inside the loop, you're going to have to check if the booking starts in the middle
                    //   of the month, or if starts at the beginning of the month.
if ($month == 12) {
$next_month = 1;
$what_year = $year+1;
}
else
$next_month++;
// If $end_of_booking is a UNIX timestamp
while ($end_of_booking >= time(0, 0, 0, 1, $next_month, $what_year)) {
/* Process the payment at next month's rate, you can use the above code to help you with this
      keep in mind though - the above works specifically for the 1st month in the booking, you'll have to
      to check to see if the booking ends during the "$next_month" variable or if it goes past it. If it goes past
      just process from the begining of $next_month to the end of $next_month, otherwise process from the
      beginning of $next_month to the end of the booking. */

if ($next_month == 12) {
  $next_month = 1;
  $what_year = $what_year+1;
}
else
  $next_month++;
}

 

Hope that helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.