hi i have a little bit of issue just thingking if somebody can give me a hand out here..

the issue is when i compute for a car rate..it will depend on the date of rental and the return date,which is already coverd the problem is it will not really just depend on the date but also in time of return

e.g
format(yyyy-mm-d)
startdate=2012-06-01
pickup hour=06:00

enddate=2012-06-02
return hour=06:00

rate:2500

but if the return date is already 5hour past than the pickup hour then rate=5000

e.g
startdate=2012-06-01
pickup hour=06:00

enddate=2012-06-02
return hour=11:00

rate:5000

here is the code in computing for the rate by day,week and month..but no idea how to compute the excess hour..

<?php  
  $vehicle;
  date_default_timezone_set('Europe/London');

  function rate($start_date, $end_date)
{
    $interval = calculate_interval($start_date, $end_date);
    // Total months
    $months = floor($interval / DAYS_IN_MONTH);
    $interval = $interval % DAYS_IN_MONTH;
    // Total weeks
    $weeks = floor($interval / DAYS_IN_WEEK);
    $interval = $interval % DAYS_IN_WEEK;
    // Total days
    $days = $interval;
    // Calculate total cost
    return ($months * COST_PER_MONTH) + ($weeks * COST_PER_WEEK) + ($days * COST_PER_DAY);
}
/**
 * Find total number of days passed between start and end dates inclusive.
 *
 * @param string $start_date
 * @param string $end_date
 * @return integer
 */
function calculate_interval($start_date, $end_date)
{
    return floor((strtotime($end_date) - strtotime($start_date) + SECONDS_IN_DAY) / SECONDS_IN_DAY);
}
$start_date = '2012-06-01';
$pickuphour='06:00';
$end_date = '2012-06-02';
$returnhour='11:00';

if($vehicle=="Toyota VIOS 1.3 MT")
{

define('SECONDS_IN_DAY', 86400);
define('DAYS_IN_MONTH', 30);
define('DAYS_IN_WEEK', 7);
define('COST_PER_MONTH', 52500);
define('COST_PER_WEEK', 15000);
define('COST_PER_DAY', 2500);

}

if($vehicle=="Toyota VIOS 1300 E AT")
{

define('SECONDS_IN_DAY', 86400);
define('DAYS_IN_MONTH', 30);
define('DAYS_IN_WEEK', 7);
define('COST_PER_MONTH', 63000);
define('COST_PER_WEEK', 18000);
define('COST_PER_DAY', 3000);


}
if($vehicle=="Toyota VIOS 1500 G AT")
{

define('SECONDS_IN_DAY', 86400);
define('DAYS_IN_MONTH', 30);
define('DAYS_IN_WEEK', 7);
define('COST_PER_MONTH', 69300);
define('COST_PER_WEEK', 19800);
define('COST_PER_DAY', 3300);


}
if($vehicle=="Toyota ALTIS 1600 E MT")
{

define('SECONDS_IN_DAY', 86400);
define('DAYS_IN_MONTH', 30);
define('DAYS_IN_WEEK', 7);
define('COST_PER_MONTH', 73500);
define('COST_PER_WEEK', 21000);
define('COST_PER_DAY', 3500);


}
if($vehicle=="Toyota ALTIS 1600 G AT")
{

define('SECONDS_IN_DAY', 86400);
define('DAYS_IN_MONTH', 30);
define('DAYS_IN_WEEK', 7);
define('COST_PER_MONTH', 84000);
define('COST_PER_WEEK', 24000);
define('COST_PER_DAY', 4000);


}
if($vehicle=="Toyota INNOVA 2.0 J GAS MT")
{

define('SECONDS_IN_DAY', 86400);
define('DAYS_IN_MONTH', 30);
define('DAYS_IN_WEEK', 7);
define('COST_PER_MONTH', 84000);
define('COST_PER_WEEK', 24000);
define('COST_PER_DAY', 4000);


}
if($vehicle=="Toyota INNOVA J Dsl MT")
{

define('SECONDS_IN_DAY', 86400);
define('DAYS_IN_MONTH', 30);
define('DAYS_IN_WEEK', 7);
define('COST_PER_MONTH', 94500);
define('COST_PER_WEEK', 27000);
define('COST_PER_DAY', 4500);


}

$rate=rate($start_date, $end_date);
?>

typo error in my statement

what i mean is

the issue is when i compute for a car rate..it will depend on the date of rental and the return date,which is already coverd the problem is it will not really just depend on the date but also in time of return

e.g
format(yyyy-mm-d)
startdate=2012-06-01
pickup hour=06:00

enddate=2012-06-02
return hour=06:00

rate:2500

but if the return hour is already 5hour past than the pickup hour then rate=5000

e.g
startdate=2012-06-01
pickup hour=06:00

enddate=2012-06-02
return hour=11:00

rate:5000

any suggestion or samples..

That should be simple enough if all you want to do is charge an additional day if the customer doesn't return the vehicle on or before the time they collected the vehicle.

<?php

date_default_timezone_set('Europe/London');

define('SECONDS_IN_DAY', 86400);
define('DAYS_IN_MONTH', 30);
define('DAYS_IN_WEEK', 7);
define('COST_PER_MONTH', 52500);
define('COST_PER_WEEK', 15000);
define('COST_PER_DAY', 2500);


$start_date = '2012-06-01 06:00:00';
$end_date = '2012-06-12 11:00:00';

echo calculate_cost($start_date, $end_date);

/**
 * Find total rental cost for period covered by start and end dates inclusive.
 *
 * @param string $start_date
 * @param string $end_date
 * @return float
 */
function calculate_cost($start_date, $end_date)
{
    $interval = calculate_interval($start_date, $end_date);

    // Total months
    $months = floor($interval / DAYS_IN_MONTH);
    $interval = $interval % DAYS_IN_MONTH;

    // Total weeks
    $weeks = floor($interval / DAYS_IN_WEEK);
    $interval = $interval % DAYS_IN_WEEK;

    // Total days
    $days = $interval;

    // Calculate total cost
    return ($months * COST_PER_MONTH) + ($weeks * COST_PER_WEEK) + ($days * COST_PER_DAY);
}

/**
 * Find total number of days passed between start and end dates inclusive.
 *
 * @param string $start_date
 * @param string $end_date
 * @return integer
 */
function calculate_interval($start_date, $end_date)
{
    return ceil((strtotime($end_date) - strtotime($start_date) + SECONDS_IN_DAY) / SECONDS_IN_DAY);
}

The only change is in the calculate_interval function, where floor has changed to ceil.

hi blocblue its you again.. thank you for the quick reply and sorry if im bothering you again..

im still having a wrong output when i put

startdate=2012-06-01
pickup hour=06:00

enddate=2012-06-02
return hour=06:00

im getting rate:5000 which is wrong it should be rate:2500 because it is just equivalent to 24hrs

it will only change to rate:5000 when the return hour=11:00 or higher..

Okay, ignore the previous change I suggested. I missed that you wanted to allow a 5 hour grace period.

Please also note that 2012-06-01 06:00:00 - 2012-06-02 06:00:00 is not 24 hours. Technically it is 24 hours and 1 second, because the 24 hour period would have finished at 2012-06-02 05:59:59.

I have posted new code below. This works on the aforementioned basis for when a 24 hour period will finish. It also allows you to specify a grace period in seconds. In this case, 5 hours is 1,800 seconds.

R.

<?php

date_default_timezone_set('Europe/London');

define('SECONDS_IN_DAY', 86400);
define('DAYS_IN_MONTH', 30);
define('DAYS_IN_WEEK', 7);
define('COST_PER_MONTH', 52500);
define('COST_PER_WEEK', 15000);
define('COST_PER_DAY', 2500);
define('GRACE_PERIOD', 1800);


$start_date = '2012-06-01 06:00:00';
$end_date = '2012-06-02 05:59:59';

echo calculate_cost($start_date, $end_date);

/**
 * Find total rental cost for period covered by start and end dates inclusive.
 *
 * @param string $start_date
 * @param string $end_date
 * @return float
 */
function calculate_cost($start_date, $end_date)
{
    $interval = calculate_interval($start_date, $end_date, GRACE_PERIOD);

    // Total months
    $months = floor($interval / DAYS_IN_MONTH);
    $interval = $interval % DAYS_IN_MONTH;

    // Total weeks
    $weeks = floor($interval / DAYS_IN_WEEK);
    $interval = $interval % DAYS_IN_WEEK;

    // Total days
    $days = $interval;

    // Calculate total cost
    return ($months * COST_PER_MONTH) + ($weeks * COST_PER_WEEK) + ($days * COST_PER_DAY);
}

/**
 * Find total number of days passed between start and end dates inclusive.
 *
 * @param string $start_date
 * @param string $end_date
 * @param integer $margin
 * @return integer
 */
function calculate_interval($start_date, $end_date, $margin = 0)
{
    return floor((strtotime($end_date) - $margin - strtotime($start_date) + SECONDS_IN_DAY) / SECONDS_IN_DAY);
}

again thank you so much blocblue for the great help..still cant say that my problem is over yet..but then again thanks bro your the man..

You're welcome.

What problems are you still having, or does the above solve your current problem?

yah it already solve my current problem..but then im still seeing some small flaws you see when i try to put 2012-06-01 and 2012-06-01 im getting 0 for rate..i am just thinking if it is when 2012-06-01 and 2012-06-01 it will have a default rate as 2500..but the other from 2012-06-01 to 2012-06-02 up i have no problem anymore

Hmmm... I expect the issue you're experiencing is because of the grace period. Are you always passing a date / time value to the calculate_cost function?

Will the grace period always be 5 hours?

R.

Member Avatar for diafol

As blocblue has got his teeth into this one, I don't want to hijack this and suggest something else (he's more experienced at this stuff than me!), but I'd just like to point out a possible problem with using 'seconds' to calculate weeks etc. I came across the problem when calculating date differences during leap years and Summertime Daylight Saving. Using the php datetime->diff() or datediff corrected these issues. Just a thought.

@blocblue:yeah the grace period is always 5hrs from the pick up date

ex
startdate:2012-06-01
pick up hour:09:00:00
startdate:2012-06-02
return hour:17:00:00

rate:5000

but if
startdate:2012-06-01
pick up hour:09:00:00
startdate:2012-06-02
return hour:09:00:00

rate:2500

aside from that its already working fine thanks to you..

@diafol: yeah i agree with you i can say that blocbro have things for number..and sad to say i dont have that thing and i will never have that..

Sorry for the slow reply - busy day yesterday.

I've been looking at your problem further and have hopefully found a solution that covers all bases.

From what I understand, a customer a minimum of one day for renting a vehicle. If they return the vehicle within the first 5 hours after the original time that they rented a vehicle, but on a subsequent day, they're not charged an additional day - i.e. a grace period.

The new code I've posted below calculates two intervals.

One is what I call a standard interval, whereby the difference in complete days is found between the collection and return time (deducting an additional second to account for your collect at 6am, return at 6am, rather than 5:59am next day being one day).

The second is what I call a grace interval, whereby the same calculation for the standard interval is performed, but a 5 hour deduction is made to the return time - the grace period.

I am then comparing the two intervals. If the grace period interval is at least one day, and is less than the standard interval, then that is returned. Otherwise the standard interval is returned.

When testing, this seems to work correctly. Hopefully the code is clear enough.

<?php

date_default_timezone_set('Europe/London');

define('SECONDS_IN_DAY', 86400);
define('DAYS_IN_MONTH', 30);
define('DAYS_IN_WEEK', 7);
define('COST_PER_MONTH', 52500);
define('COST_PER_WEEK', 15000);
define('COST_PER_DAY', 2500);
define('GRACE_PERIOD', 18000);


$start_date = '2012-06-01 06:00:00';
$end_date = '2012-06-02 06:00:00';

echo calculate_cost($start_date, $end_date);

/**
 * Find total rental cost for period covered by start and end dates inclusive.
 *
 * @param string $start_date
 * @param string $end_date
 * @return float
 */
function calculate_cost($start_date, $end_date)
{
    $interval = calculate_interval($start_date, $end_date);

    // Total months
    $months = floor($interval / DAYS_IN_MONTH);
    $interval = $interval % DAYS_IN_MONTH;

    // Total weeks
    $weeks = floor($interval / DAYS_IN_WEEK);
    $interval = $interval % DAYS_IN_WEEK;

    // Total days
    $days = $interval;

    // Calculate total cost
    return ($months * COST_PER_MONTH) + ($weeks * COST_PER_WEEK) + ($days * COST_PER_DAY);
}

/**
 * Find total number of days passed between start and end dates inclusive.
 *
 * @param string $start_date
 * @param string $end_date
 * @return integer
 */
function calculate_interval($start_date, $end_date)
{
    // Find standard interval and grace period interval
    $standard_interval = floor((strtotime($end_date) - strtotime($start_date) + SECONDS_IN_DAY - 1) / SECONDS_IN_DAY);
    $grace_interval = floor((strtotime($end_date) - strtotime($start_date) + SECONDS_IN_DAY - GRACE_PERIOD - 1) / SECONDS_IN_DAY);

    // Return grace interval if greater than 0 and less than standard interval. Otherwise standard interval
    return ($grace_interval && $standard_interval > $grace_interval) ? $grace_interval : $standard_interval;
}

CooOl...hey blocblue great work..yeah it is working great now..thanks alot man...im so in debt with you man your the best,great coding..hope i can be good as so when it came to this kind of stuff..thank you:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.