Reputation: 83
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
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
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