我正在尝试根据日期计算 1-21 天的价格。
家庭控制器
$Sql = ' SELECT DISTINCT
a.property_id, a.date, a.minimum_stay,
a.maximum_stay,a.quantity,
a.arrival_allowed,a.departure_allowed,
p.duration, p.persons, p.amount,
p.extra_person_price, p.minimum_stay AS price_minimum_stay,
p.maximum_stay AS price_maximum_stay, p.weekdays,
p.period_till, p.period_from,
datediff(p.period_till, p.period_from) AS number_of_days
FROM availabilities AS a
JOIN prices AS p
ON a.property_id=p.property_id
WHERE a.minimum_stay >0
AND a.maximum_stay < 22
AND a.date >= p.period_from
AND a.date <= p.period_till
';
$Stm = $this->getEntityManager()->getConnection()->prepare($Sql);
$Stm->execute();
return $Stm->fetchAll(PDOConnection::FETCH_ASSOC);
public function CalculatePrice($persons, $extra_person_price, $date, $amount, $duration, $period_till, $period_from)
{
//loop through persons
foreach ($persons as $person) {
//calculate price for persons
if ($person > 1) {
$amount += $person * $extra_person_price;
}
//array to link parameters with database fields
$tmp = array(
"date" => $date,
"person" => $person,
"price_person" => number_format($amount / 100, 2, '.', ',')
);
//loop through $tmp an add value from 2 to 21 to day an add this to $tmp array with calculated value
//$x=$number_of_days;
//$days = (strtotime($period_till) - strtotime($period_from)) / (60 * 60 * 24);
for ($x = 1; $x <= 21; ++$x) {
if ($x >1) {
$tmp["day$x"] = $tmp["day".($x-1)] + number_format($amount / 100, 2, '.', ',');
//number_format(($amount * $x) / 100, 2, '.', ',');
} else {
$tmp["day$x"] = "0.00";
}
$price_per_person[] = $tmp;
}
return $price_per_person;
}
我也在计算人数的价格,但这部分很好。现在我已经做了一个 for 循环并存储了从 1 到 21 的数字天数请参阅我的第二个 for 循环function CalculatePrice
。但这部分不好,我需要根据日期来计算。例如:
大部分时间的常规价格为 123 欧元。假设 9 月 3 日每天费用为 250 欧元,9 月 7 日每天费用为 300 欧元。假设一个人想停留 5 天,他于 9 月 3 日抵达,那么计算结果为:250 + 123 + 123 + 123 + 300 = 919 欧元。
但我需要这个基于日期。我尝试过 period_from 和 period_til 但到目前为止没有运气。有人可以举个例子或一些有用的提示,我如何在 for 循环中做到这一点,就像我的第二个 for 循环一样。