RD Mage
RD Mage

Reputation: 103

PHP round datetime to nearest 5 interval

Following is the requirement, i have given datetime in format

11/14/2017 22:36

it should be round of to the nearest 5 minutes interval like following:

11/14/2017 22:36  => Output Should be 11/14/2017 22:40
11/14/2017 11:23  => Output Should be 11/14/2017 11:25

Upvotes: 1

Views: 1512

Answers (1)

Beginner
Beginner

Reputation: 4153

Thanks to this post https://stackoverflow.com/a/26103014/3481654

The formula for that is

$time = round(time() / 300) * 300;

In complete working code

function nearest5Mins($time) {
  $time = (round(strtotime($time) / 300)) * 300;
  return date('Y-M-d H:i', $time);
}
echo nearest5Mins('11/14/2017 22:48');
echo "<br>";
echo nearest5Mins('11/14/2017 11:23');

DEMO

Upvotes: 3

Related Questions