Learn how to round down/up the minutes of a DateTime object in PHP by some interval.

In some applications, on demanding applications with standardized methodologies, storing DateTime objects in the database is an absolute art. But, in which sense? That they need to be stored in some specifical format, caring about accuracy and strictness. Let's take as example as scheduler application that displays events in blocks of 5 minutes, this calendar only accepts the time of the event in such format e.g 5:25, 6:45 etc. If another time is shown, the calendar will fail, so is up to the server side to provide a correct format of time. With the DateTime class of PHP is pretty easy to modify a date, however it would be not very intuitive to round the time of a DateTime object by some interval e.g 5 or 10 minutes.

In this article, we'll share with you 3 methods that will help you to round the time of a DateTime object to its closest interval, highest or lowest easily.

Note

All three methods have as default 10 minutes as interval, you can change it as you want e.g 5, 20 etc. Just change the interval as second argument of the functions and that's it!

Round to nearest interval

The following function rounds the minutes of a datetime object to the closest given interval:

<?php 

/**
 * Round minutes to the nearest interval of a DateTime object.
 * 
 * @param \DateTime $dateTime
 * @param int $minuteInterval
 * @return \DateTime
 */
public function roundToNearestMinuteInterval(\DateTime $dateTime, $minuteInterval = 10)
{
    return $dateTime->setTime(
        $dateTime->format('H'),
        round($dateTime->format('i') / $minuteInterval) * $minuteInterval,
        0
    );
}

And it can be used like this:

<?php 

$date = new DateTime("2018-06-27 20:37:00");

$date = roundToNearestMinuteInterval($date);

// Rounded from 37 minutes to 40
//  2018-06-27 20:40:00
echo $date->format("Y-m-d H:i:s");

Or in case that the minutes of the datetime object are lower than the highest interval, the lower will be used:

<?php 

$date = new DateTime("2018-06-27 20:33:00");

$date = roundToNearestMinuteInterval($date);

// Rounded from 33 minutes to 30
//  2018-06-27 20:30:00
echo $date->format("Y-m-d H:i:s");

Round to highest interval

The following function rounds the minutes of a datetime object to the highest given interval:

<?php 

/**
 * Round up minutes to the nearest upper interval of a DateTime object.
 * 
 * @param \DateTime $dateTime
 * @param int $minuteInterval
 * @return \DateTime
 */
public function roundUpToMinuteInterval(\DateTime $dateTime, $minuteInterval = 10)
{
    return $dateTime->setTime(
        $dateTime->format('H'),
        ceil($dateTime->format('i') / $minuteInterval) * $minuteInterval,
        0
    );
}

And it can be used like this:

<?php 

$date = new DateTime("2018-06-27 20:37:00");

$date = roundUpToMinuteInterval($date);

// Rounded from 37 minutes to 40
//  2018-06-27 20:40:00
echo $date->format("Y-m-d H:i:s");

Round to lowest interval

The following function rounds the minutes of a datetime object to the lowest given interval:

<?php

/**
 * Round down minutes to the nearest lower interval of a DateTime object.
 * 
 * @param \DateTime $dateTime
 * @param int $minuteInterval
 * @return \DateTime
 */
public function roundDownToMinuteInterval(\DateTime $dateTime, $minuteInterval = 10)
{
    return $dateTime->setTime(
        $dateTime->format('H'),
        floor($dateTime->format('i') / $minuteInterval) * $minuteInterval,
        0
    );
}

And it can be used like this:

<?php 

$date = new DateTime("2018-06-27 20:33:00");

$date = roundDownToMinuteInterval($date);

// Rounded from 33 minutes to 30
//  2018-06-27 20:30:00
echo $date->format("Y-m-d H:i:s");

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors