Symfony calculate number of days based on date

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I am trying to calculate price for number of days from 1-21 based on date.

HomeController

$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;     } 

I am also calculating price for number of persons but that part is good. Right now I have made a for loop and stored numbers from 1 to 21 for number of days see my second for loop in function CalculatePrice. But this part not good I need to calculate this based on date. for example: Regularly price for most of days is 123 Euro. let say on 3 September a day cost 250 euro and on 7 September it cost 300 Euro. So let say A person want to stay for 5 days and he arrives at 3 September so the calculation will be: 250 + 123 + 123 + 123 + 300 = 919 Euro. But I need this to be based on date. I have tried with period_from and period_til but so far no luck. Can someone give an example or some useful hints how I can do this in a for loop like my second for loop.

回答1:

I have solved this question based on the theory T. Abdelmalek gave me. I made an array and stored date and price of each date in and loped through. The answer has been deleted somehow I don't know how to mark as solved



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!