Jump to content

defining special date blocks


dflow

Recommended Posts

i am building a reservation system

i have special date blocks which have different price tags

 

for example reservation for

01/12/2011-07/12/2011

 

where

03/12/2011-06/12/2011 prices +15%

 

how would i define the range for special dates?

 

Link to comment
Share on other sites

Didn't you already do that here?

03/12/2011-06/12/2011 prices +15%

 

If you are asking how you would implement that in your application, there is no way for us to give you an answer as we don't have a clue of how your application is currently configured. If you are using a database, then you could simply have a table to define date ranges with up/down charges. I would use (at a minimum) three fields: start_date, end_date, rate_change. How you would use that in calculations would again depend on how your application is currently built.

Link to comment
Share on other sites

Didn't you already do that here?

03/12/2011-06/12/2011 prices +15%

 

If you are asking how you would implement that in your application, there is no way for us to give you an answer as we don't have a clue of how your application is currently configured. If you are using a database, then you could simply have a table to define date ranges with up/down charges. I would use (at a minimum) three fields: start_date, end_date, rate_change. How you would use that in calculations would again depend on how your application is currently built.

 

hi i did ask this in the past

yes but it doesnt answer the question of specific days in that block

 

i get from my search form

$start_date='01/12/2011';
$end_date='07/12/2011';
03/12/2011-06/12/2011 prices +15%
//so regarding the example
$special_rate=$price+15%;
$regular_days=3
$special_days=3
$result=($regular_days*$price)+($special_days*($price+15%))

 

how would you extract these block dates?

Link to comment
Share on other sites

OK, so the question isn't

how would i define the range for special dates?

 

It is how you would apply the rates. There is still a lot of information not available. For example, is the normal rate flat (i.e. it is the same for every day) or does it vary (e.g. weekends are normally more). Also, can there by more than one "special date block"? All of that will determine how this should be coded. I will assume that if there are multiple "special date blocks" that they do not overlap.

 

Here is a sample script that works (AGAIN: If there are "special date blocks" that overlap, they will all be applied. But since I did not provide any logic on how multiple rate differentials should be applied if there is an overlap, the results might not be what you intend.) This was also done against my test database - so you may need to make some modifications for this to work for you. This only returns the total rate for the reservation period. If you need a breakout, you can modify the function accordingly.

 

function getRate($startDate, $endDate, $normalRate)
{
    //Modify dates to timestamps
    $startDateTS = strtotime($startDate);
    $endDateTS   = strtotime($endDate);
    //Get total days for reservation period
    $totalDays = intval(($endDateTS - $startDateTS) / 86400) + 1;
    //Get rate for total days at normal rate
    $rateTotal = $totalDays * $normalRate;

    //Run query to get number of days to apply for each
    //special date block rates within reservation period
    $query = "SELECT rate
                     DATEDIFF(LEAST(rate_end_date, FROM_UNIXTIME({$endDateTS})),
                              GREATEST(rate_start_date, FROM_UNIXTIME({$startDateTS}))) as rate_days
              FROM `date_blocks`
              WHERE rate_end_date   >= FROM_UNIXTIME({$startDateTS})
                AND rate_start_date <= FROM_UNIXTIME({$endDateTS})";
    $result = mysql_query($query);

    //Adjust rate total for number of days in each date block
    while($rateBlock = mysql_fetch_assoc($result))
    {
        $rateTotal = $rateTotal + ($normalRate * $rateBlock['rate_days'] * $rateBlock['rate']);
    }

    //Return the total cost for the reservation period
    return $rateTotal;
}

//Reservation dates
$start_date = '01/12/2011';
$end_date = '07/12/2011';

//Get rate total
$rateTotal = getRate($start_date, $end_date, 20);

Link to comment
Share on other sites

OK, so the question isn't

how would i define the range for special dates?

 

It is how you would apply the rates. There is still a lot of information not available. For example, is the normal rate flat (i.e. it is the same for every day) or does it vary (e.g. weekends are normally more). Also, can there by more than one "special date block"? All of that will determine how this should be coded. I will assume that if there are multiple "special date blocks" that they do not overlap.

 

Here is a sample script that works (AGAIN: If there are "special date blocks" that overlap, they will all be applied. But since I did not provide any logic on how multiple rate differentials should be applied if there is an overlap, the results might not be what you intend.) This was also done against my test database - so you may need to make some modifications for this to work for you. This only returns the total rate for the reservation period. If you need a breakout, you can modify the function accordingly.

 

function getRate($startDate, $endDate, $normalRate)
{
    //Modify dates to timestamps
    $startDateTS = strtotime($startDate);
    $endDateTS   = strtotime($endDate);
    //Get total days for reservation period
    $totalDays = intval(($endDateTS - $startDateTS) / 86400) + 1;
    //Get rate for total days at normal rate
    $rateTotal = $totalDays * $normalRate;

    //Run query to get number of days to apply for each
    //special date block rates within reservation period
    $query = "SELECT rate
                     DATEDIFF(LEAST(rate_end_date, FROM_UNIXTIME({$endDateTS})),
                              GREATEST(rate_start_date, FROM_UNIXTIME({$startDateTS}))) as rate_days
              FROM `date_blocks`
              WHERE rate_end_date   >= FROM_UNIXTIME({$startDateTS})
                AND rate_start_date <= FROM_UNIXTIME({$endDateTS})";
    $result = mysql_query($query);

    //Adjust rate total for number of days in each date block
    while($rateBlock = mysql_fetch_assoc($result))
    {
        $rateTotal = $rateTotal + ($normalRate * $rateBlock['rate_days'] * $rateBlock['rate']);
    }

    //Return the total cost for the reservation period
    return $rateTotal;
}

//Reservation dates
$start_date = '01/12/2011';
$end_date = '07/12/2011';

//Get rate total
$rateTotal = getRate($start_date, $end_date, 20);

 

thanks ill run it through now i understand the table settings

, and your correction to the definition of my question is what i meant

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.