Adamhumbug Posted August 18, 2023 Share Posted August 18, 2023 (edited) Hi All, I have a rental system - if you rent for 3 or more days over a week you pay for 2 weeks, 3 or more days over 2 weeks and you pay for 3 and so on. As i need to be able to see how many weeks someone is renting equipment for i have built a js function but wondered if there was a more effience way of getting the weeks value than the following. if ($days == 0){ $weeks = 0 }else if ($days < 10) { $weeks = 1 } else if ($days >= 10 && $days < 16) { $weeks = 2 } else if ($days >= 17 && $days < 23) { $weeks = 3 } else if ($days >= 24 && $days < 30) { $weeks = 4 } Thanks as always Edited August 18, 2023 by Adamhumbug Quote Link to comment https://forums.phpfreaks.com/topic/317201-more-efficient-rounding-calculator/ Share on other sites More sharing options...
Barand Posted August 18, 2023 Share Posted August 18, 2023 Try $weeks = intdiv($days, 7) + ($days%7 > 3 ? 1 : 0); Quote Link to comment https://forums.phpfreaks.com/topic/317201-more-efficient-rounding-calculator/#findComment-1611251 Share on other sites More sharing options...
Adamhumbug Posted August 18, 2023 Author Share Posted August 18, 2023 8 minutes ago, Barand said: Try $weeks = intdiv($days, 7) + ($days%7 > 3 ? 1 : 0); Amazing - as ever!! Quote Link to comment https://forums.phpfreaks.com/topic/317201-more-efficient-rounding-calculator/#findComment-1611254 Share on other sites More sharing options...
Barand Posted August 18, 2023 Share Posted August 18, 2023 Correction: > 3 should be >= 3 Quote Link to comment https://forums.phpfreaks.com/topic/317201-more-efficient-rounding-calculator/#findComment-1611255 Share on other sites More sharing options...
Adamhumbug Posted August 18, 2023 Author Share Posted August 18, 2023 Sorry my testing was wrong here - i am gettin intdiv is undefined. Looking into it - will report back once tested properly. Quote Link to comment https://forums.phpfreaks.com/topic/317201-more-efficient-rounding-calculator/#findComment-1611256 Share on other sites More sharing options...
Adamhumbug Posted August 18, 2023 Author Share Posted August 18, 2023 Ah, this is a PHP function? I am in JS at the mo Quote Link to comment https://forums.phpfreaks.com/topic/317201-more-efficient-rounding-calculator/#findComment-1611257 Share on other sites More sharing options...
Solution Barand Posted August 18, 2023 Solution Share Posted August 18, 2023 The code you posted was using PHP variable names. ($days, $weeks) intdiv (x, y) == floor(x/y) Quote Link to comment https://forums.phpfreaks.com/topic/317201-more-efficient-rounding-calculator/#findComment-1611259 Share on other sites More sharing options...
Adamhumbug Posted August 18, 2023 Author Share Posted August 18, 2023 19 minutes ago, Barand said: The code you posted was using PHP variable names. ($days, $weeks) intdiv (x, y) == floor(x/y) Apologies, i am using JQuery. $weeks = Math.floor($days/7) + ($days%7 >= 3 ? 1 : 0); The above is working beautifully. Quote Link to comment https://forums.phpfreaks.com/topic/317201-more-efficient-rounding-calculator/#findComment-1611261 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.