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
https://forums.phpfreaks.com/topic/285692-php-math-help/
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)";
Link to comment
https://forums.phpfreaks.com/topic/285692-php-math-help/#findComment-1466636
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.