Php 找到下一个小时最近的

Php 找到下一个小时最近的,php,Php,如何在php中找到下一个最近的小时 例如,如果当前时间是4:15,下一个小时将是5点,以此类推 $dateString = 'Tue, 13 Mar 2012 04:48:34 -0400'; $date = new DateTime( $dateString ); echo $date->format( 'H:i:s' ); 给我字符串中的时间,我想在此基础上展开,得到下一个最接近的小时,你能拿几段(小时、分钟、秒)来得到下一个小时吗 $dateString = 'Tue, 13 Ma

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

例如,如果当前时间是4:15,下一个小时将是5点,以此类推

$dateString = 'Tue, 13 Mar 2012 04:48:34 -0400';
$date = new DateTime( $dateString );
echo $date->format( 'H:i:s' );
给我字符串中的时间,我想在此基础上展开,得到下一个最接近的小时,你能拿几段(小时、分钟、秒)来得到下一个小时吗

$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";
将任何符合条件的日期()提供给:


因为我需要类似的东西(下一个完整小时),所以我的解决方案如下:

$now = time();
$nextFullHour = date(DATE_ATOM, $now + (3600 - $now % 3600));
$dateTime = new \DateTime();
$dateTime->add(new \DateInterval('PT1H'))
         ->setTime($dateTime->format('H'), '00');
通过将
3600
替换为
60
您将获得下一整分钟的时间…

如果不需要相对于当前时间的
$now
时间戳,也可以将其替换为任何其他时间戳。

如果需要将第二个参数置于日期函数中,请尝试当前时间

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

非常好的东西

这是我的解决方案:

$now = time();
$nextFullHour = date(DATE_ATOM, $now + (3600 - $now % 3600));
$dateTime = new \DateTime();
$dateTime->add(new \DateInterval('PT1H'))
         ->setTime($dateTime->format('H'), '00');
我们开始:

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

还有一个:

$current_datetime=new datetime不可变();
$next\u full\u hour\u datetime=$current\u datetime
->修改(
斯普林特(
“+%d秒”,
3600-($current_datetime->getTimestamp()%3600)
)
);

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

$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' ) );

这次聚会有点晚了,但这里有一个更灵活的函数,可以将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' );
    }

简单、快捷、有效。如果当前小时数=24。然后next=24+1=25我认为这是最好的。我同意,这应该被接受。。。很简单。
    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' );
    }