"switch" with intervals to snap to a 15-minute mark I am working with dates and times which are not necessarily "on point", which means that while I need the times in fixed quarterly sections, I can't make sure that the saved timestamp isn't a little off.

I need xx:00, xx:15, xx:30 and xx:45 obviously, but the times could be yy:03, yy:14 and alike, you get the hang of it...

Normally I would use a switch statement to select what is about to happen, but in PHP switch does not work with intervals.

Currently, I am stuck with this code:

$temp_minute = date('i', $tdd-&gt;datum);

if( ( $temp_minute &gt; 10 ) &amp;&amp; ( $temp_minute &lt; 20 ) ) { $minute = '15'; }
elseif( ( $temp_minute &gt; 25 ) &amp;&amp; ( $temp_minute &lt; 35 ) ) { $minute = '30'; }
elseif( ( $temp_minute &gt; 40 ) &amp;&amp; ( $temp_minute &lt; 50 ) ) { $minute = '45'; }
else { $minute = '00'; }


This seems to be far from elegant but gets the job done.

Are there any "nicer" ways around this, given the case that I might need to add more sections or am I bound to elseif statements?
You can do it 'mathematically' like this:

function fixMinutesToQuarter($minutes) {
  return sprintf('%02d',15*(round($minutes/15) % 4));
}


What does it do?


Divide by 15 so the minutes fall in a range from 0.0 to 3.933. 
Rounding so it is either 0, 1, 2, 3 or 4. 
Take the 'modulo' so 4 becomes 0. See:
http://php.net/manual/en/language.operators.arithmetic.php 
Multiply by 15. 
Format the output to be two digits with leading zero.


I tested it with this code:

for ($minutes = 0;$minutes &lt; 60;$minutes++) {
  echo $minutes.' gives '.fixMinutesToQuarter($minutes).'&lt;br&gt;';
}


It returns:

0 gives 00       15 gives 15        30 gives 30       45 gives 45
1 gives 00       16 gives 15        31 gives 30       46 gives 45
2 gives 00       17 gives 15        32 gives 30       47 gives 45
3 gives 00       18 gives 15        33 gives 30       48 gives 45
4 gives 00       19 gives 15        34 gives 30       49 gives 45
5 gives 00       20 gives 15        35 gives 30       50 gives 45
6 gives 00       21 gives 15        36 gives 30       51 gives 45
7 gives 00       22 gives 15        37 gives 30       52 gives 45
8 gives 15       23 gives 30        38 gives 45       53 gives 00
9 gives 15       24 gives 30        39 gives 45       54 gives 00
10 gives 15      25 gives 30        40 gives 45       55 gives 00
11 gives 15      26 gives 30        41 gives 45       56 gives 00
12 gives 15      27 gives 30        42 gives 45       57 gives 00
13 gives 15      28 gives 30        43 gives 45       58 gives 00
14 gives 15      29 gives 30        44 gives 45       59 gives 00


In case you can have negative minutes, or minutes > 59 you should limit the range.

If you still prefer your 'if .... else' conditional approach you could now simplify it to:

if     ($temp_minute &gt; 52) $minute = '00';
elseif ($temp_minute &gt; 37) $minute = '45';
elseif ($temp_minute &gt; 22) $minute = '30';
elseif ($temp_minute &gt; 7)  $minute = '15';
else                       $minute = '00';


This can be further shortened by using the ternary operator, but it will then become quite unreadable, and not any faster, so I wouldn't do that.

Personally I prefer the 'mathematical' approach.