AJLX Posted January 26, 2014 Share Posted January 26, 2014 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 Any help or tips would be lovely! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/285692-php-math-help/ Share on other sites More sharing options...
Ch0cu3r Posted January 26, 2014 Share Posted January 26, 2014 (edited) 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 January 26, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/285692-php-math-help/#findComment-1466636 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.