user3034899
user3034899

Reputation: 83

PHP Time: get next minute/hour what's divisible by five

I need a code what detect the current time and give me the following time what is divisible by five.

For example: It's: 06 03, 2017 21:22:23 , give me 06 03, 2017 21:25:00 in php. I have no any idea to starting. Is there any code or solution to do this?

Upvotes: 3

Views: 287

Answers (2)

Mat
Mat

Reputation: 2428

Find the difference between 5 and modulo of current minutes. Then add this to current time. Example:

$now = intval(date('i'));
$minutesToAdd = 5 - $now % 5;
$nextTime = date('m d, Y H:i:00', strtotime("+$minutesToAdd minutes"));
echo $nextTime;

Maybe it's not perfect solution, but shuld work, also for cases like 13:59 (modulo is 4, 5-4=1, 59+1=00).

Upvotes: 0

Grant
Grant

Reputation: 6319

$now = time();
// Add five minutes in seconds, round it down to the nearest five:
$next = ceil($now/300)*300; 
echo date('H:i', $next);

Upvotes: 2

Related Questions