douxie2029 2013-07-18 12:11
浏览 35
已采纳

php - 计算有特殊价格的天数

My site is going to be for booking sun loungers per day in hotels and parks. Normally the loungers cost a default price per day, but sometimes there is a peak rate (e.g. holiday season or weekends). So I have a table

special_prices
--------
start_date
end_date
price

And I have a search/calculator function which allows users to enter a start date and end date of when they'd like to hire a lounger and the calculator figures out a total price including the special rates.

Each lounger has it's own record, so I have all the special_price records associated with a particular lounger in an array, and I figured I should loop through each of these records and if the user inputted days fall between a special_price record's dates then I somehow need to count how many days need to have the increased amount added to it.

I'm having trouble figuring this out as I'm new to php and really just doing this for the learning experience. I've been fiddling with it for too long now though :(

  • 写回答

1条回答 默认 最新

  • dongmei9508 2013-07-18 12:37
    关注

    This problem is usually solved by SQL Stored Procedures. But since you tagged your question as php, here is a php answer:

    // Let's imagine that $db is a PDO instance
    
    // fetch all special prices
    $stmt = $db->query('SELECT * FROM `special_prices`;');
    $specialPrices = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    // init datetime objects
    $startDate = new \DateTime('16.05.2013');
    $endDate = new \DateTime('08.06.2013');
    $currentDate = clone $startDate;
    
    // set default price and init result price (set it to 0)
    $defaultPrice = 10;
    $resultPrice = 0;
    
    while ($currentDate <= $endDate)
    {
        // init price the will be added to teh result as a default one
        $addPrice = $defaultPrice;
    
        foreach ($specialPrices as $specialPrice)
        {
            // temp special price DateTime objects to compare them with the current date
            $specialPriceStartDate = new \DateTime($specialPrice['start_date']);
            $specialPriceEndDate = new \DateTime($specialPrice['end_date']);
    
            if ($currentDate >= $specialPriceStartDate && $currentDate <= $specialPriceEndDate)
            {
                // If one of special dates matches with the current date, set its price as $addPrice
                $addPrice = $specialPrice['price'];
                break;
            }
        }
    
        // add price (default or special as calculated before) to the result
        $resultPrice += $addPrice;
    
        // get the next day
        $currentDate->modify('+1 day');
    }
    
    // here is the result
    echo $resultPrice;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了