PHP:3个不同定价的时段
这都是关于预订的PHP:3个不同定价的时段,php,calculator,Php,Calculator,这都是关于预订的 我有三个时间段: 我已经做了一个php计算,计算每个时段的预订天数和定价。但我不知道如何计算两个周期之间的时间。 例如: 某人从2014年1月26日至2014年4月25日预订=89天*价格1 但如果有人从第三阶段到第一阶段为。。。我试图分开计算: if ($check_in >= '2013-04-30' && $check_out <= '2014-09-21') { //Days and price calcs here
我有三个时间段:
我已经做了一个php计算,计算每个时段的预订天数和定价。但我不知道如何计算两个周期之间的时间。 例如:
某人从2014年1月26日至2014年4月25日预订=89天*价格1
但如果有人从第三阶段到第一阶段为。。。我试图分开计算:
if ($check_in >= '2013-04-30' && $check_out <= '2014-09-21')
{
//Days and price calcs here
// contains Period 2 and Period 1
}
如果($check\u in>='2013-04-30'&&$check\u out首先,我假设$check\u in
和$check\u out
是从某种形式获得的字符串,然后将它们与另一个字符串进行比较,其中任何一个都是日期
您可以将$check\u in
和$check\u out
转换为Datetime,然后进行比较,例如:
// 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\u in
是一个日期间隔对象,您可以检查天数,例如,如果小时数大于0,则$check\u in晚于比较日期,如果小于0,则$check\u in早于此
if($diff_in->h >= 0 and $diff_out->h <= 0){
// We are within this date range.
}
您可以为每一天生成一个价格。然后从开始日期循环到结束日期,并对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个时间段签入并签出到第2个时间段(或第3个时间段)时它不返回任何内容,因为我无法加入所有时段:(还有其他建议吗?谢谢,但对我不起作用。我错过了一个非常重要的问题。再次检查问题,我编辑了它。谢谢,但它对我不起作用。我错过了一个非常重要的问题。再次检查问题,我编辑了它。当它出现时,一切都很完美,特别是在period、 但是,当有人从时段1签入并签出到时段2(或时段3)时,它不会返回任何内容,因为我无法加入所有时段:(
if($diff_in->h >= 0 and $diff_out->h <= 0){
// We are within this date range.
}
DateInterval Object
(
[y] => 0
[m] => 0
[d] => 0
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 0
)
<?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);