一切都与预订有关...
我有 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 计算来计算每个时期的预订天数和定价。但我不知道如何在两个时期之间进行计算。
例如:
有人从 2014 年 1 月 26 日到 2014 年 4 月 25 日期间预订 = 89 天 * 价格 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。
最佳答案
您可以生成每天的价格。然后从开始日期循环到结束日期,并对日价格求和以获得总价:
<?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);
关于PHP:3个时间段,不同的定价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21366626/