Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find the closest next hour

Tags:

php

how can I find the next closest hour in php

so for example if current time is 4:15 the next hour will be 5, etc

$dateString = 'Tue, 13 Mar 2012 04:48:34 -0400';
$date = new DateTime( $dateString );
echo $date->format( 'H:i:s' );

gives me the time from the string and I want to expand on that and get the next closest hour

like image 408
Asim Zaidi Avatar asked Mar 13 '12 09:03

Asim Zaidi


3 Answers

$nextHour = (intval($date->format('H'))+1) % 24;
echo $nextHour; // 5
like image 93
Frosty Z Avatar answered Oct 30 '22 21:10

Frosty Z


Here we go:

<?php
    echo date("H:00",strtotime($date. " + 1hour "));
?>
like image 8
SlavisaPetkovic Avatar answered Oct 30 '22 19:10

SlavisaPetkovic


Can you just take pieces (hours, minutes, seconds) and get the next hour?

$dateString = 'Tue, 13 Mar 2012 04:48:34 -0400';
$date = new DateTime( $dateString );

echo $date->format( 'H:i:s' );
echo "\n";

$nexthour = ($date->format('H') + ($date->format('i') > 0 || $date->format('s') > 0 ? 1 : 0)) % 24;
echo "$nexthour:00:00";
like image 5
Aleks G Avatar answered Oct 30 '22 20:10

Aleks G