Jump to content

Php Math help


AJLX

Recommended Posts

Hey guys,

 

I'm trying to work out some logic for my hire system. I'm trying to work out how much to charge for an item.

 

At the moment I'm taking the start date, the end date, and putting the range into an array. I'm then counting that array to find out the total amount of time that the hire lasts for. If a hire lasts for a day, then I charge a day rate. If the hire lasts more than 1.5 days, but less than 7, then a week price is charged.

 

If a hire lasts 8 days, then I would charge a week rate, and a day rate. Anymore then It would become a second week.

 

I don't really know where to start with this? I've never been good at maths :confused:

 

Any help or tips would be lovely!

 

Thanks!

Link to comment
Share on other sites

If you know the exact dates, then pass them to DateTime:diff

 

This can return the difference in days between the two dates. You'll multiply this by your daily charge rate. Example code

// dates given in year-month-day hour:min:sec format
$datetime1 = new DateTime('2009-10-11 00:00:00'); // date 1
$datetime2 = new DateTime('2009-10-19 12:00:00'); // date 2
$interval = $datetime1->diff($datetime2);
$totalDays = $interval->format('%a');
$hourDiff = $interval->format('%h');

// divide hour difference and by 24, adding this to total days
if($hourDiff > 0)
	$totalDays += ($hourDiff / 24);

// daily charge rate
$chareRate = 12.50; 

// charge for 1 day.
if($totalDays == 1)
{
	$total = $chareRate;
	$perioid = 'day';
}
// charge for a week, when total days is greater than 1 day (noon) till 7 days
elseif($totalDays >= 1.5 && $totalDays <= 7)
{
	$total = $chareRate * 7;
	$period = 'week';
}
// charge for 1 week and a day for 8 days
elseif(floor($totalDays) == 
{
	$total = $totalDays * $chareRate;
	$period = 'week +1 day';

}
// charge for two weeks if total days is greater than 8 days
elseif($totalDays > 
{
	$total = $chareRate * 14;
	$period = 'fortnight';
}

echo "Rental costs for $totalDays days @ $chareRate = $total (Rental Period $period)";
Edited by Ch0cu3r
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.