找到最近的下一个小时


find the closest next hour

如何在php 中找到下一个最接近的小时

例如,如果当前时间是4:15,那么下一个小时将是5,等等

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

给了我字符串中的时间,我想对此进行扩展,并获得下一个最接近的小时

$nextHour = (intval($date->format('H'))+1) % 24;
echo $nextHour; // 5

开始:

<?php
    echo date("H:00",strtotime($date. " + 1hour "));
?>

你能只取碎片(小时、分钟、秒)并获得下一个小时吗?

$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";

向提供任何符合条件的日期()

function roundToNextHour($dateString) {
    $date = new DateTime($dateString);
    $minutes = $date->format('i');
    if ($minutes > 0) {
        $date->modify("+1 hour");
        $date->modify('-'.$minutes.' minutes');
    }
    return $date;
}
<?php
$dateString = 'Tue, 13 Mar 2012 04:48:34 -0400';
$date = new DateTime( $dateString );
$date->modify('+1 hour');
echo $date->format('H:i:s').PHP_EOL;
// OR
echo date('H:i:s', strtotime($dateString) + 60 * 60).PHP_EOL;

由于我只是需要类似的东西(接下来整整一个小时),所以我的解决方案是:

$now = time();
$nextFullHour = date(DATE_ATOM, $now + (3600 - $now % 3600));

通过用60替换3600,您可以获得下一个完整的分钟
如果相对于当前时间不需要$now,也可以将其替换为任何其他时间戳。

这就是我的解决方案:

$dateTime = new 'DateTime();
$dateTime->add(new 'DateInterval('PT1H'))
         ->setTime($dateTime->format('H'), '00');

没有其他人使用过这个,所以我想把它放在这里,这是我上面看到的最简单的一个实际时间戳,而不仅仅是小时本身。

$now = ''; // empty uses current time, or you can insert a datetime string
$next_hour = date( 'Y-m-d H:00:00', strtotime( $now . ' +1 hour' ) );

如果您需要将第二个参数放入日期函数,请在当前时间尝试

<?php echo date('H')+1; ?>    

非常好的东西

还有一个:

$current_datetime = new DateTimeImmutable();
$next_full_hour_datetime = $current_datetime
    ->modify(
        sprintf(
            '+%d seconds',
            3600 - ($current_datetime->getTimestamp() % 3600)
        )
    );

这次聚会有点晚,但这里有一个更灵活的函数,可以按分钟为单位的任何间隔对dateTime对象进行四舍五入。你输入了dateTime对象和以分钟为单位的舍入间隔,所以在一个小时内,你只需要输入60等

    public function round_up_time( $datetime, $rounding_interval ) {
        // get total minutes from the start of the day
        $minutes = ( intval( $datetime->format( 'H' ) ) * 60 ) + ( intval( $datetime->format( 'i' ) ) );
        // round up the minutes based on the interval we are rounding to
        $rounded_minutes = ( intval( $minutes / $rounding_interval ) + 1 ) * $rounding_interval;
        // reset our dateTime to the very start of the day
        $datetime->setTime( 0, 0 );
        // then increase the dateTime by the rounded minutes value
        $datetime->modify( '+ ' . $rounded_minutes . ' minutes' );
    }

要在DateTime中获得下一个最接近的完整小时:

$date = new DateTime('+1 hour'); //set the next hour
$date->setTime($date->format('H'), '00', '00'); //keep the next hour, set minutes to 00 and seconds to 00

简单如下:

$nextHourTS = strtotime(date('Y-m-d H:00:00',strtotime('+1 hours')));
$nextHourDate = date('Y-m-d H:i:s',$nextHourTS);

strtotime是相对于时区的,所以请记住这一点。祝你今天过得愉快!