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 :(

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;