Rounding number to the nearest interval

So I have a predefined interval, maybe 5, 10, 15, etc.

If someone enters 20, I need to round it based on the interval. So if it was a 15 minute interval, it would automatically move to 30, if it was a 45 minute interval, it would go to 45

Basically, anything <= interval becomes interval; all intervals between intervals become the next largest interval

I don't want to write some confusing php function to do this when there may be a simple way that I just don't know about.

+5
source share
3 answers

Rounding will be:

$ceiled = $interval * ceil( $value / $interval);
+13
source
roundedInput  = (Ceiling( Input / Interval)) * Interval

So, given input 20 and interval 15, you get:

20/15 = 1.33

1.33 rounded = 2

2 * 15 = 30

0

$Rounded = Ceil ($ value/$interval) * $interval;

0

All Articles