简体   繁体   中英

PHP: 3 time periods with different pricing

It's all about booking...

I've got 3 time periods:

 Period 3 from 01.01.2014 to 29.04.2014 - Price 3 (per day) Period 2 from 30.04.2014 to 14.06.2014 - Price 2 (per day) Period 1 from 15.06.2014 to 21.09.2014 - Price 1 (per day) Period 4 from 22.09.2014 to 29.04.2015 - Price 3 (per day) 

I've already made a php calculation that calculates the booked days and pricing for each period. But I can't figure it out how to calculate between two periods.

For example:
Somebody books from 26.01.2014 to 25.04.2014 = 89 days * Price 1

But it gets really hard when somebody books from period 3 to period 1 for... I tried to separate the calculations:

   if ($check_in >= '2013-04-30' && $check_out <= '2014-09-21')
     {
    //Days and price calcs here
    // contains Period 2 and Period 1
     }

But it doesn't work well...

Do you have any ideas how to make the whole calculation to work perfectly?

I missed something really important.

Here is the structure:

Period 1

if($numberDays == 1)
{
$price = $price1_period1
}

if($numberDays >= 2 && $numberDays <= 3)
{

$price = $price2_period1 * $numberDays;
}

if($numberDays >= 4 && $numberDays <= 6)
{
$price = $price3_period1 * $numberDays;
}

if($numberDays >= 7 && $numberDays <= 14)
{
$price = $price4_period1 * $numberDays;
}

if($numberDays >= 15 && $numberDays <= 29)
{

$price = $price5_period1 * $numberDays;
} 

if($numberDays >= 30)
{
$price = $price6_period1 * $numberDays;
}


It's the same for the other periods.
Ex.: for period 2 the price for 6 days is $price3_period2.

You could generate a price for each day. Then loop from start date to end date and sum the dayprice to get the total price:

<?php
$oneDay = 24*3600;

$configs = array(
    array(
        'startTime' => strtotime('2014-01-01'),
        'endTime' => strtotime('2014-04-29'),
        'price' => 10
    ),
    array(
        'startTime' => strtotime('2014-04-30'),
        'endTime' => strtotime('2014-06-14'),
        'price' => 20
    ),
    array(
        'startTime' => strtotime('2014-06-15'),
        'endTime' => strtotime('2014-09-21'),
        'price' => 30
    ),
    array(
        'startTime' => strtotime('2014-09-22'),
        'endTime' => strtotime('2015-04-29'),
        'price' => 40
    ),
);

$prices = array();
foreach ($configs as $config)
{
    $time1 = $config['startTime'];
    $time2 = $config['endTime'];
    $price = $config['price'];

    while ($time1 <= $time2)
    {
        $prices[date('Y-m-d', $time1)] = $price;
        $time1 += $oneDay;
    }
}

/**
 * @param $checkIn in format YYYY-mm-dd
 * @param $checkOut in format YYYY-mm-dd
 */
function getTotalPrice($checkIn, $checkOut, $prices)
{
    $time1 = strtotime($checkIn);
    $time2 = strtotime($checkOut);

    $price = 0;
    while ($time1 <= $time2)
    {
        $time1 += 24 * 3600;
        $price += $prices[date('Y-m-d', $time1)];
    }

    return $price;
}

echo getTotalPrice('2014-01-04', '2014-01-09', $prices);

First things first, I assume $check_in and $check_out are strings that you get from some form, then you are comparing them with another string, any of them are dates.

What you can do is convert both $check_in and $check_out to Datetime and then do the comparison, example:

// Check In Date
$check_in_date = new Datetime($check_in);
$date_compare_in = new Datetime('2013-04-30');

$diff_in = $check_in_date->diff($date_compare_in);

// Check Out Date
$check_out_date = new Datetime($check_out);
$date_compare_out = new Datetime('2014-09-21');

$diff_out = $check_out_date->diff($date_compare_out);

Now $diff_in is a DateInterval object that you can check for the quantity of days, example, if the hours are greater than 0, the $check_in was later than the compare date, if is less than 0 the $check_in was before.

if($diff_in->h >= 0 and $diff_out->h <= 0){
// We are within this date range.
}

a DateInterval Object has the following structure:

DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 0
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 0
    [days] => 0
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM