ds211107 2017-08-22 12:34
浏览 7
已采纳

php更好的计算天数的方法

Home controller:

//my query in repository
 public function Available (){
 $Sql= ' SELECT DISTINCT 
        a.property_id, a.date, a.minimum_stay,
        a.maximum_stay,a.quantity,
        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 
        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

 ';
    class HomeController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */

    public function indexAction(Request $request)
    {
        $hotel_information = $this->retrieveInformationFromDB();

        return $this->render('Home/index.html.twig', array(
                'hotel_information'=>$hotel_information
            )
        );
    }


    private function retrieveInformationFromDB(){
        $em= $this->getDoctrine()->getEntityManager();
        $availables = $em->getRepository('AppBundle:Availabilities')->Available();
        $hotel_information=array();

        for($i = 0; $i< count($availables) ; $i++) {
            $date = $availables[$i]['date'];
            $duration = $availables[$i]['duration'];
            $amount = $availables[$i]['amount'];
            $extra_person_price = $availables[$i]['extra_person_price'];
            $persons = explode('|',$availables[$i]['persons']);

            $hotel_information_placeholder = $this->calculatePricePerPerson($persons, $extra_person_price, $date, $amount,$duration);
            $hotel_information = array_merge($hotel_information, $hotel_information_placeholder);

        }

        return $hotel_information;
    }

    private function calculatePricePerPerson($persons, $extra_person_price, $date, $amount,$duration){
        foreach ($persons as $person) {
            if ($person > 1) {
                $price_per_person[] = array(
                    "date" => $date,
                    "person" => $person,
                    "price_person" => number_format((($person * $extra_person_price) + $amount) / 100, 2, '.', ','),
                    "day2"=>number_format(((($person * $extra_person_price) + $amount)*2) / 100, 2, '.', ','),
                    "day3"=>number_format(((($person * $extra_person_price) + $amount)*3) / 100, 2, '.', ','),
                    "day4"=>number_format(((($person * $extra_person_price) + $amount)*4) / 100, 2, '.', ','),
                    "day5"=>number_format(((($person * $extra_person_price) + $amount)*5) / 100, 2, '.', ','),
                    "day6"=>number_format(((($person * $extra_person_price) + $amount)*6) / 100, 2, '.', ','),
                    "day7"=>number_format(((($person * $extra_person_price) + $amount)*7) / 100, 2, '.', ','),

                );

            } else {
                $price_per_person[] = array(
                    "date" => $date,
                    "person" => $person,
                    "price_person" => number_format($amount / 100, 2, '.', ','),
                    "day2"=>number_format(($amount * 2)/ 100, 2, '.', ','),
                    "day3"=>number_format(($amount * 3)/ 100, 2, '.', ','),
                    "day4"=>number_format(($amount * 4)/ 100, 2, '.', ','),
                    "day5"=>number_format(($amount * 5)/ 100, 2, '.', ','),
                    "day6"=>number_format(($amount * 6)/ 100, 2, '.', ','),
                    "day7"=>number_format(($amount * 7)/ 100, 2, '.', ',')

                );
            }
        }

        return $price_per_person;
    }

In function private function calculatePricePerPerson I am calculating the price for a number of person and number of days based on date. At the moment I have calculated like this. The problem is a calculating number of is from 1 to 21 so therefore my foreach loop get really long and it is really ugly. So My question is how can I do this in better and more efficient way.

  • 写回答

1条回答 默认 最新

  • dongtaochan0777 2017-08-22 13:02
    关注

    This code has several redundant points that can be optimized. The original code :

    private function calculatePricePerPerson($persons, $extra_person_price, $date, $amount,$duration){
            foreach ($persons as $person) {
                if ($person > 1) {
                    $price_per_person[] = array(
                        "date" => $date,
                        "person" => $person,
                        "price_person" => number_format((($person * $extra_person_price) + $amount) / 100, 2, '.', ','),
                        "day2"=>number_format(((($person * $extra_person_price) + $amount)*2) / 100, 2, '.', ','),
                        "day3"=>number_format(((($person * $extra_person_price) + $amount)*3) / 100, 2, '.', ','),
                        "day4"=>number_format(((($person * $extra_person_price) + $amount)*4) / 100, 2, '.', ','),
                        "day5"=>number_format(((($person * $extra_person_price) + $amount)*5) / 100, 2, '.', ','),
                        "day6"=>number_format(((($person * $extra_person_price) + $amount)*6) / 100, 2, '.', ','),
                        "day7"=>number_format(((($person * $extra_person_price) + $amount)*7) / 100, 2, '.', ','),
    
                    );
    
                } else {
                    $price_per_person[] = array(
                        "date" => $date,
                        "person" => $person,
                        "price_person" => number_format($amount / 100, 2, '.', ','),
                        "day2"=>number_format(($amount * 2)/ 100, 2, '.', ','),
                        "day3"=>number_format(($amount * 3)/ 100, 2, '.', ','),
                        "day4"=>number_format(($amount * 4)/ 100, 2, '.', ','),
                        "day5"=>number_format(($amount * 5)/ 100, 2, '.', ','),
                        "day6"=>number_format(($amount * 6)/ 100, 2, '.', ','),
                        "day7"=>number_format(($amount * 7)/ 100, 2, '.', ',')
    
                    );
                }
            }
    
            return $price_per_person;
        }
    

    At first glance, it seems that the codes in both your if and else blocks are quite similar, the only difference being the extra person thing. By focusing on that element, you can avoid having two times almost the same code :

    private function calculatePricePerPerson($persons, $extra_person_price, $date, $amount,$duration){
            foreach ($persons as $person) {
                if ($person > 1) {
                    $amount += $person * $extra_person_price;
                }
                $price_per_person[] = array(
                    "date" => $date,
                    "person" => $person,
                    "price_person" => number_format($amount / 100, 2, '.', ','),
                    "day2"=>number_format(($amount * 2)/ 100, 2, '.', ','),
                    "day3"=>number_format(($amount * 3)/ 100, 2, '.', ','),
                    "day4"=>number_format(($amount * 4)/ 100, 2, '.', ','),
                    "day5"=>number_format(($amount * 5)/ 100, 2, '.', ','),
                    "day6"=>number_format(($amount * 6)/ 100, 2, '.', ','),
                    "day7"=>number_format(($amount * 7)/ 100, 2, '.', ',')
                );
            }
            return $price_per_person;
        }
    

    Instead of filling the array in each branch of the if, the person price is added to $amount. The else becomes useless and the array is filled all at once since $amount has a different value depending on the case.

    Much shorter, but it's not over. The entries day2 to day7 are almost identical, the only difference being the number at the end of the key and factor by which $amount is multiplyed. By chance, they are always the same in each line, so lets reduce it to a loop :

    private function calculatePricePerPerson($persons, $extra_person_price, $date, $amount,$duration){
            foreach ($persons as $person) {
                if ($person > 1) {
                    $amount += $person * $extra_person_price;
                }
                $tmp = array(
                    "date" => $date,
                    "person" => $person,
                    "price_person" => number_format($amount / 100, 2, '.', ',')
                );
                for ($x = 2; $x <= 7; ++$x) {
                    $tmp["day$x"] = number_format(($amount * $x)/ 100, 2, '.', ',');
                }
                $price_per_person[] = $tmp;
            }
            return $price_per_person;
        }
    

    During this step, I created a temporary array that is filled with the static data, then a for loop which adds entries to $tmp for each number of day that has to be handled. The temporary array is then assigned to $price_per_person[] so that everything works as it did in the first version. And since the days are now handled buy a loop, you can increase or decrease the amount of days that have to be computed and diplayed simply by modifying the limit in the for - currently 7.

    And that's about as short as you can get without losing clarity. Besides, by removing redundancy you can update your code at once instead of - trying to - update every occurence of the - roughly - same code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题