# Re: [PHP] bumping up hour by one

```
"Tom Beidler" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...```
```
> My mistake. The correct code that works is;
>
> \$display_time = date("H:i");
>
> // round time to nearest 15 minute interval
> \$display_timex = explode (":",\$display_time) ;
> if ((\$display_timex[1] >= 00) && (\$display_timex[1] <= 07)) {
>     \$insert_time = \$display_timex[0] . ":00";
> } elseif ((\$display_timex[1] >= 8) && (\$display_timex[1] <= 22)) {
>     \$insert_time = \$display_timex[0] . ":15";
> } elseif ((\$display_timex[1] >= 23) && (\$display_timex[1] <= 37)) {
>     \$insert_time = \$display_timex[0] . ":30";
> } elseif ((\$display_timex[1] >= 38) && (\$display_timex[1] <= 52)) {
>     \$insert_time = \$display_timex[0] . ":45";
> } elseif ((\$display_timex[1] >= 53) && (\$display_timex[1] <= 59)) {
>     \$display_timex[0]++;
>     \$insert_time = \$display_timex[0] . ":00";
> }

How 'bout:

// split it in a more readable way
list(\$hour, \$min) = split(" ", date("H i"));

// calculate nearest quarter-hour
\$quarters = (int) ((\$min + 7) / 15);

// check for round-to-next-hour
if (4 == \$quarters) {
\$quarters = 0;
\$hour++;
}

// turn quarter-hours back into minutes
\$min = 15 * \$quarters;

// format final result
\$insert_time = sprintf("%2i:%02i", \$hour, \$min);

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

```