I have the INTEGER(255) variable $duration
stored with a value taken from the user. I need to round this UP to the nearest 15. I have searched all over but haven’t been able to find a solution.
How may I go about doing this?
For example:
- 10 becomes 15
- 16 becomes 30
- 130 becomes 135
Also, how can I add $duration
to a TIME variable $time
to output the time after that duration?
For example, from 080000
:
- If
$duration
is 15,$time
becomes 081500 - If
$duration
is 30,$time
becomes 083000 - If
$duration
is 135,$time
becomes 101500
Thank you!
Advertisement
Answer
You can use the simple division and addition with casting (will return the whole value without the fraction) operator as follows:
$val = 130;//or any value $roundedVal = ((int)($val/15) + 1) * 15;
about the second question:
$time='080000'; $valHours = (int)($val/60); $valMin = (int)($val % 60); $time = $time + ($valHours * 10000) + ($valMin * 100); $time = strlen($time) == 8? $time : '0' . $time;
the last row is meant or fixing the leading zero, since it’s not a conventional time format. I haven’t took into consideration the seconds.