PHP:3个不同价格的时间段

时间:2014-01-26 17:18:23

标签: php calculator

这都是关于预订......

我有3个时间段:

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)

我已经做了一个PHP计算,计算每个时期的预订天数和定价。但我无法弄清楚如何在两个时期之间进行计算。

例如:
有人从26.01.2014 to 25.04.2014 = 89 days * Price 1

开始预订

但是当有人从第3期到第1期预订时,它变得非常困难......我试图将计算分开:

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

但它效果不好......

您是否有任何想法如何使整个计算完美运作?

我错过了一些非常重要的事情。

这是结构:

期间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;
}

其他时期也是如此。
例如:对于第2期,6天的价格是$ price3_period2。

2 个答案:

答案 0 :(得分:2)

您可以为每一天生成价格。然后从开始日期到结束日期循环并将dayprice相加以获得总价格:

<?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);

答案 1 :(得分:1)

首先,我假设$check_in$check_out是从某种形式获得的字符串,然后您将它们与另一个字符串进行比较,其中任何一个都是日期。

您可以做的是将$check_in$check_out转换为日期时间,然后进行比较,例如:

// 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);

现在$diff_in是一个DateInterval对象,您可以检查天数,例如,如果小时数大于0,$ check_in晚于比较日期,如果小于0 $ check_in就在之前。

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

DateInterval对象具有以下结构:

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