src/Carbon/Traits/Rounding.php
public function roundUnit($unit, $precision = 1, $function = 'round')
    {
        $metaUnits = [
            // @call roundUnit
            'millennium' => [static::YEARS_PER_MILLENNIUM, 'year'],
            // @call roundUnit
            'century' => [static::YEARS_PER_CENTURY, 'year'],
            // @call roundUnit
            'decade' => [static::YEARS_PER_DECADE, 'year'],
            // @call roundUnit
            'quarter' => [static::MONTHS_PER_QUARTER, 'month'],
            // @call roundUnit
            'millisecond' => [1000, 'microsecond'],
        ];
        $normalizedUnit = static::singularUnit($unit);
        $ranges = array_merge(static::getRangesByUnit(), [
            // @call roundUnit
            'microsecond' => [0, 999999],
        ]);
        $factor = 1;
        if (isset($metaUnits[$normalizedUnit])) {
            [$factor, $normalizedUnit] = $metaUnits[$normalizedUnit];
        }
        $precision *= $factor;

        if (!isset($ranges[$normalizedUnit])) {
            throw new InvalidArgumentException("Unknown unit '$unit' to floor");
        }

        $found = false;
        $fraction = 0;
        $arguments = null;
        $factor = $this->year < 0 ? -1 : 1;
        $changes = [];

        foreach ($ranges as $unit => [$minimum, $maximum]) {
            if ($normalizedUnit === $unit) {
                $arguments = [$this->$unit, $minimum];
                $fraction = $precision - floor($precision);
                $found = true;

                continue;
            }

            if ($found) {
                $delta = $maximum + 1 - $minimum;
                $factor /= $delta;
                $fraction *= $delta;
                $arguments[0] += $this->$unit * $factor;
                $changes[$unit] = round($minimum + ($fraction ? $fraction * call_user_func($function, ($this->$unit - $minimum) / $fraction) : 0));
                // Cannot use modulo as it lose double precision
                while ($changes[$unit] >= $delta) {
                    $changes[$unit] -= $delta;
                }
                $fraction -= floor($fraction);
            }
        }

        [$value, $minimum] = $arguments;
        /** @var CarbonInterface $result */
        $result = $this->$normalizedUnit(floor(call_user_func($function, ($value - $minimum) / $precision) * $precision + $minimum));
        foreach ($changes as $unit => $value) {
            $result = $result->$unit($value);
        }

        return $result;
    }
public function roundUnit($unit, $precision = 1, $function = 'round')
    {
        $metaUnits = [
            // @call roundUnit
            'millennium' => [static::YEARS_PER_MILLENNIUM, 'year'],
            // @call roundUnit
            'century' => [static::YEARS_PER_CENTURY, 'year'],
            // @call roundUnit
            'decade' => [static::YEARS_PER_DECADE, 'year'],
            // @call roundUnit
            'quarter' => [static::MONTHS_PER_QUARTER, 'month'],
            // @call roundUnit
            'millisecond' => [1000, 'microsecond'],
        ];
        $normalizedUnit = static::singularUnit($unit);
        $ranges = array_merge(static::getRangesByUnit(), [
            // @call roundUnit
            'microsecond' => [0, 999999],
        ]);
        $factor = 1;
        if (isset($metaUnits[$normalizedUnit])) {
            [$factor, $normalizedUnit] = $metaUnits[$normalizedUnit];
        }
        $precision *= $factor;

        if (!isset($ranges[$normalizedUnit])) {
            throw new InvalidArgumentException("Unknown unit '$unit' to floor");
        }

        $found = false;
        $fraction = 0;
        $arguments = null;
        $factor = $this->year < 0 ? -1 : 1;
        $changes = [];

        foreach ($ranges as $unit => [$minimum, $maximum]) {
            if ($normalizedUnit === $unit) {
                $arguments = [$this->$unit, $minimum];
                $fraction = $precision - floor($precision);
                $found = true;

                continue;
            }

            if ($found) {
                $delta = $maximum + 1 - $minimum;
                $factor /= $delta;
                $fraction *= $delta;
                $arguments[0] += $this->$unit * $factor;
                $changes[$unit] = round($minimum + ($fraction ? $fraction * call_user_func($function, ($this->$unit - $minimum) / $fraction) : 0));
                // Cannot use modulo as it lose double precision
                while ($changes[$unit] >= $delta) {
                    $changes[$unit] -= $delta;
                }
                $fraction -= floor($fraction);
            }
        }

        [$value, $minimum] = $arguments;
        /** @var CarbonInterface $result */
        $result = $this->$normalizedUnit(floor(call_user_func($function, ($value - $minimum) / $precision) * $precision + $minimum));
        foreach ($changes as $unit => $value) {
            $result = $result->$unit($value);
        }

        return $result;
    }
public function roundUnit ( $ unit , $ precision = 1 , $ function = 'round' ) { $ metaUnits = [ // @call roundUnit 'millennium' => [ static :: YEARS_PER_MILLENNIUM , 'year' ] , // @call roundUnit 'century' => [ static :: YEARS_PER_CENTURY , 'year' ] , // @call roundUnit 'decade' => [ static :: YEARS_PER_DECADE , 'year' ] , // @call roundUnit 'quarter' => [ static :: MONTHS_PER_QUARTER , 'month' ] , // @call roundUnit 'millisecond' => [ 1000 , 'microsecond' ] , ] ; $ normalizedUnit = static :: singularUnit ( $ unit ) ; $ ranges = array_merge ( static :: getRangesByUnit ( ) , [ // @call roundUnit 'microsecond' => [ 0 , 999999 ] , ] ) ; $ factor = 1 ; if ( isset ( $ metaUnits [ $ normalizedUnit ] ) ) { [ $ factor , $ normalizedUnit ] = $ metaUnits [ $ normalizedUnit ] ; } $ precision *= $ factor ; if ( ! isset ( $ ranges [ $ normalizedUnit ] ) ) { throw new InvalidArgumentException ( "Unknown unit '$unit' to floor" ) ; } $ found = false ; $ fraction = 0 ; $ arguments = null ; $ factor = $ this -> year < 0 ? - 1 : 1 ; $ changes = [ ] ; foreach ( $ ranges as $ unit => [ $ minimum , $ maximum ] ) { if ( $ normalizedUnit === $ unit ) { $ arguments = [ $ this -> $ unit , $ minimum ] ; $ fraction = $ precision - floor ( $ precision ) ; $ found = true ; continue ; } if ( $ found ) { $ delta = $ maximum + 1 - $ minimum ; $ factor /= $ delta ; $ fraction *= $ delta ; $ arguments [ 0 ] += $ this -> $ unit * $ factor ; $ changes [ $ unit ] = round ( $ minimum + ( $ fraction ? $ fraction * call_user_func ( $ function , ( $ this -> $ unit - $ minimum ) / $ fraction ) : 0 ) ) ; // Cannot use modulo as it lose double precision while ( $ changes [ $ unit ] >= $ delta ) { $ changes [ $ unit ] -= $ delta ; } $ fraction -= floor ( $ fraction ) ; } } [ $ value , $ minimum ] = $ arguments ; /** @var CarbonInterface $result */ $ result = $ this -> $ normalizedUnit ( floor ( call_user_func ( $ function , ( $ value - $ minimum ) / $ precision ) * $ precision + $ minimum ) ) ; foreach ( $ changes as $ unit => $ value ) { $ result = $ result -> $ unit ( $ value ) ; } return $ result ; }
Round the current instance at the given unit with given precision if specified and the given function.

@param string    $unit
@param float|int $precision
@param string    $function

@return CarbonInterface
Round the current instance at the given unit with given precision if specified and the given function .