17

我正在从 mysql 字段中检索日期时间,但我需要将其四舍五入最接近的 10 分钟。

例如,如果日期时间是 2013-11-06 14:00:01,我想将时间返回为 6/11/2013 14:10。

最简单的方法是什么?

$datetime = new DateTime($mysqldata);

echo $datetime->format('d/m/Y G:i');

任何建议表示赞赏。

谢谢。

4

10 回答 10

20

1) 如有必要,将秒数设置为 0(通过四舍五入到最接近的分钟)

$second = $datetime->format("s");
if($second > 0)
    $datetime->add(new DateInterval("PT".(60-$second)."S"));

2) 获取分钟

$minute = $datetime->format("i");

3)转换模10

$minute = $minute % 10;

4)如有必要,将分钟数到接下来的 10 分钟

if($minute != 0)
{
    // Count difference
    $diff = 10 - $minute;
    // Add difference
    $datetime->add(new DateInterval("PT".$diff."M"));
}

已编辑,感谢@Ondrej Henek 和@berturion

于 2013-11-06T14:30:30.793 回答
15

我偶然发现了这个问题,想知道为什么所有的解决方案都如此复杂。就是这样:

function roundToNext10Min(\DateTime $dt, $precision = 10) {
    $s = $precision * 60;
    $dt->setTimestamp($s * (int) ceil($dt->getTimestamp() / $s));
    return $dt;
}

$dt = roundToNext10Min(new DateTime($str));
echo $dt->format('d/m/Y G:i');
于 2016-11-30T14:58:26.447 回答
14

编辑:我很久以前写过这个答案,有些人发现不考虑毫秒。值得尝试除我之外的其他答案来找到完美的时间舍入方法。

当我们要舍入的 DateTime 已经是 10 分钟的倍数时,@rand 的答案有一个错误。它会在不需要时跳到 10 分钟后。

($datetime->add(new \DateInterval("PT".(60-$second)."S"));此外,当秒数已经为零时,总是不希望跳到下一分钟。

此增强功能修复了这些错误并采用 $precision 参数来涵盖更多用例。

function roundTime(\DateTime &$datetime, $precision = 30, $round_lower = false) {
    // 1) Set number of seconds to 0 (by rounding up to the nearest minute if necessary)
    $second = (int) $datetime->format("s");
    if ($second > 30 && $round_lower == false) {
        // Jumps to the next minute
        $datetime->add(new \DateInterval("PT".(60-$second)."S"));
    } elseif ($second > 0) {
        // Back to 0 seconds on current minute
        $datetime->sub(new \DateInterval("PT".$second."S"));
    }
    // 2) Get minute
    $minute = (int) $datetime->format("i");
    // 3) Convert modulo $precision
    $minute = $minute % $precision;
    if ($minute > 0) {
        if($round_lower) {
            $datetime->sub(new \DateInterval("PT".$minute."M"));
        } else {
            // 4) Count minutes to next $precision-multiple minutes
            $diff = $precision - $minute;
            // 5) Add the difference to the original date time
            $datetime->add(new \DateInterval("PT".$diff."M"));
        }
    }
}

这个功能现在非常适合我使用,可能也适合其他人。

编辑:现在它允许设置圆形上或下

于 2015-10-18T08:09:44.717 回答
8

您可以使用 ceil、floor 和 round 函数将 DateTime 向上、向下或四舍五入到最接近的分钟间隔,如下所示:

/**
 * @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
    );
}

/**
 * @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
    );
}

/**
 * @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
    );
}
于 2016-10-17T11:00:05.340 回答
4

我会使用时间戳:

<?php

function roundTime(\DateTime $datetime, $precision = 10) {

    $ts = $datetime->getTimestamp();
    $s = ($precision * 60);
    $remainder = $ts % $s;

    if ($remainder > 0) {
        $datetime->setTimestamp($ts + $s - $remainder);
    }

    return $datetime;
}

echo roundTime(\DateTime::createFromFormat('Y-m-d H:i:s', '2016-03-04 09:00:00'))
    ->format('Y-m-d H:i:s');
// prints: 2016-03-04 09:00:00


echo roundTime(\DateTime::createFromFormat('Y-m-d H:i:s', '2016-03-04 09:00:01'))
    ->format('Y-m-d H:i:s');
// prints: 2016-03-04 09:10:00
于 2016-03-04T18:41:19.413 回答
1
public function roundupMinutesFromDatetime(\Datetime $date, $minuteOff = 10)
{
    $string = sprintf(
         "%d minutes %d seconds", 
         $date->format("i") % $minuteOff, 
         $date->format("s")
    );

    return  $date->sub(\DateInterval::createFromDateString($string));
}

和测试:

public function datetimeProvider()
{
    return array(
        array('2015-01-01 15:16:17', '2015-01-01 15:10:00'),
        array('2015-01-01 09:59:39', '2015-01-01 09:50:00'),
    );
}

/**
 * @dataProvider datetimeProvider
 */
public function testRoundupToMinutesFromDatetime($dateStr, $dateStrAssert)
{
    $format = 'Y-m-d H:i:s';

    $date = \DateTime::createFromFormat($format, $dateStr);
    $date = $this->repo->roundupMinutesFromDatetime($date);
    $this->assertEquals($dateStrAssert, $date->format('Y-m-d H:i:s'));
}

玩得开心。

于 2014-02-24T17:48:55.803 回答
0

例如,您需要使用strtotime()函数将日期转换为时间戳,然后添加或删除分钟

<?php
$datenow  = date("Y-m-d H:i:s", time());
$maketime = strtotime($datenow );

$minutes     = 10;

$after_10min = date("Y-m-d H:i:s", ( $maketime+(60*$minutes) ));
$before_10min = date("Y-m-d H:i:s", ( $maketime-(60*$minutes) ));

echo "Date now : $datenow  <br />";
echo "Date After $minutes min : $after_10min <br />";
echo "Date Before $minutes min : $before_10min <br />";
?>
于 2013-11-06T14:53:10.277 回答
0

这是一个将四舍五入(向上、向下或只是简单地四舍五入)到任意数量的秒、分钟或小时的函数。灵感来自@esqilin 的回答

许多博萨人为了给你带来这些信息而死。

PHP 7,虽然可能不难调整为 5。

/**
 * Round $datetime to nearest <$precision> <$unit>s - e.g nearest 10 seconds -- or always up/down per $calc
 * @param \DateTimeInterface $datetime
 * @param int    $precision Round to the nearest number of {$unit}s
 * @param string $unit      'h', 'm', or 's' for Hours, Minutes, or Seconds; or PHP's 'G'/'H' (hours) or 'i' (minutes)
 * @param int    $calc      -1, 0, or 1 = floor, round, or ceiling calculations
 * @return \DateTimeInterface
 * @throws \Exception
 */
function roundTime( \DateTimeInterface $datetime, int $precision = 1, string $unit = 's', int $calc = 0 ) :\DateTimeInterface {
    $offset = $datetime->format( 'Z' );
    $datetime = ( clone $datetime )->modify( "+$offset seconds" );
    switch( $unit ) {   // convert human readable units to PHP DateTime format string
        case 'h':
        case 'H':
            $unit = 'G';
            break;
        case 'm':
            $unit = 'i';
            break;
        // 's' -> 's', no change
    }
    $secs = [ 'G'=>3600, 'i'=>60, 's'=>1 ]; // seconds per hour/minute/second
    if( ! in_array( $unit, array_keys( $secs ) ) ) {
        trigger_error( "Unknown \$unit parameter '$unit' in " . __FUNCTION__ . ". Recognized formats are 'h', 'm', 's', (hours/minutes/seconds), or PHP's 'G', 'H', or 'i'" );
        return $datetime;
    }
    $precision = $precision * $secs[$unit];
    if( -1 === $calc ) {
        $round = 0;
    } elseif( 1 === $calc ) {
        $round = $precision;
    } elseif( 1 == $precision && 's' == $unit ) { // in this -- and only this -- case, we need partial seconds
        $round = ( (int) $datetime->format('v') ) >= 500 ? 1 : 0;
    } else {
        $round = ( ( (int) $datetime->format('U') ) % $precision >= ( $precision/2 ) ) ? $precision : 0;
    }
    $result = $datetime->setTimestamp( ( $precision * floor( $datetime->format('U') / $precision ) ) + $round - $offset );
    return $result;
}

在问题中提出的情况下,您将使用:

$datetime = new DateTime($mysqldata);

echo roundTime( $datetime, 10, 'm', 1 );
于 2019-08-07T17:06:16.947 回答
0

简单的代码

<?php
    function next10Minutes()  {
        $time = time() + 600;

        $nextMinutes = floor(date("i", $time) / 10) * 10;

        if ($nextMinutes < 10) {
            $nextMinutes = "00";
        }

        return strtotime(date("d-m-Y H:{$nextMinutes}:00", $time));
    }
于 2017-04-18T09:28:42.200 回答
-1

我重载了类并创建了一个方法:

class MyWish_DateTime extends DateTime {

public function formatToNearRangeMinutesSql($range) {
        $midleRange = $range / 2;

        $minutes = self::format("i");
        $modulo = $minutes % $range;
        if ($modulo > $midleRange) {
            $this->addMinutes($range - $modulo);
        }
        else {
            $this->addMinutes(-$modulo);
        }
        return self::format("H:i");
    }
}
于 2018-03-16T13:00:08.983 回答