PHP round time() up (future) to next multiple of 5 minutes
Asked Answered
M

6

12

How do I round the result of time() up (towards the future) to the next multiple of 5 minutes?

Myca answered 13/4, 2012 at 23:59 Comment(3)
nearest in the future or in the past?Supposititious
@hakre: you forget the option for the nearest nearest, like usual rounding works. At 14:06, the nearest 5 minutes is in the past (14:05) and at 14:08, the nearest 5 minutes is in the future (14:10). In the original (non-edited) question, it was clearly the expected email delivery time (which must be in the future).Volsung
@JarrodRoberson That's MySQL, this is just PHP.Chirography
A
48
 $now = time();     
 $next_five = ceil($now/300)*300;

This will give you the next round five minutes (always greater or equal the current time).

I think that this is what you need, based on your description.

Allot answered 14/4, 2012 at 0:18 Comment(3)
OP asked for "nearest" This will round up even if one second above a five minute mark.Alvaroalveolar
I know, that why I added the comment. by the description of his need what he's looking for is the NEXT round 5 minutes, and not the nearest (he said "script that would tell them when their email would be delivered" - future tense). maybe I'm wrong but that what I understoodAllot
Sorry about the confusion, I did indeed mean in the future and shortly after posting this realised that ceil existed, but thanks for the input!Myca
C
17

Try:

$time = round(time() / 300) * 300;
Clinandrium answered 14/4, 2012 at 0:3 Comment(2)
While the OP asked for "nearest" the context is described as future.Alvaroalveolar
This solution is valid for nearest. Use round for nearest and ceil for nearest future. While we're here, floor should work for last.Paraprofessional
H
11

Try this function:

function blockMinutesRound($hour, $minutes = '5', $format = "H:i") {
   $seconds = strtotime($hour);
   $rounded = round($seconds / ($minutes * 60)) * ($minutes * 60);
   return date($format, $rounded);
}

//call 
 blockMinutesRound('20:11');// return 20:10
Hackler answered 29/9, 2014 at 15:5 Comment(0)
P
3

For people using Carbon (such as people using Laravel), this can help:

/**
 * 
 * @param \Carbon\Carbon $now
 * @param int $nearestMin
 * @param int $minimumMinutes
 * @return \Carbon\Carbon
 */
public static function getNearestTimeRoundedUpWithMinimum($now, $nearestMin = 30, $minimumMinutes = 8) {
    $nearestSec = $nearestMin * 60;
    $minimumMoment = $now->addMinutes($minimumMinutes);
    $futureTimestamp = ceil($minimumMoment->timestamp / $nearestSec) * $nearestSec; 
    $futureMoment = Carbon::createFromTimestamp($futureTimestamp);
    return $futureMoment->startOfMinute();
}

These test assertions pass:

public function testGetNearestTimeRoundedUpWithMinimum() {
    $this->assertEquals('2018-07-07 14:00:00', TT::getNearestTimeRoundedUpWithMinimum(Carbon::parse('2018-07-06 14:12:59'), 60, 23 * 60 + 10)->format(TT::MYSQL_DATETIME_FORMAT));
    $this->assertEquals('2018-07-06 14:15:00', TT::getNearestTimeRoundedUpWithMinimum(Carbon::parse('2018-07-06 14:12:59'), 15, 1)->format(TT::MYSQL_DATETIME_FORMAT));
    $this->assertEquals('2018-07-06 14:30:00', TT::getNearestTimeRoundedUpWithMinimum(Carbon::parse('2018-07-06 14:12:59'), 30, 10)->format(TT::MYSQL_DATETIME_FORMAT));
    $this->assertEquals('2018-07-06 16:00:00', TT::getNearestTimeRoundedUpWithMinimum(Carbon::parse('2018-07-06 14:52:59'), 60, 50)->format(TT::MYSQL_DATETIME_FORMAT));
    $this->assertEquals(Carbon::parse('tomorrow 15:00:00'), TT::getNearestTimeRoundedUpWithMinimum(Carbon::parse('16:30'), 60, 60 * 22 + 30));
}
Pestilent answered 5/6, 2019 at 20:48 Comment(0)
A
2

For using Carbon:

Carbon::createFromTimestamp(round(time() / 300) * 300)
Avouch answered 29/7, 2021 at 4:55 Comment(0)
S
0

I just leave this here because maybe someone is looking for the same solution. It's more complicated than I thought to just round up the actual Carbon Time to the next 5 minutes.

Here is my solution:

private function currentTimeRoundedUpToNext5Minutes($time = null): Carbon
{
    $now = $time ? Carbon::parse($time) : Carbon::now();

    // If the actual minute is divisible by 5 we want to add 5 minutes
    if ($now->minute % 5 == 0) {
        $now->minute += 5;
        $now->second(0);

        return $now;
    }

    // If it's not divisible by 5 we need to round up to next 5 minutes
    $roundedMinute = ceil($now->minute / 5) * 5;

    // If rounded minute is equal or greater than 60 - add an hour and set minutes to 0
    if ($roundedMinute >= 60) {
        $now->addHour();
        $now->minute = 0;
    } else {
        $now->minute = $roundedMinute;
    }

    $now->second(0);

    return $now;
}
Syphon answered 26/4 at 10:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.