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 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数