Search code examples
phplaraveldatetimephp-carbon

What is the difference between ceilMinute and ceilMinutes in PHP Carbon?


In the Carbon.php source file that comes with Laravel 8 it shows

 * @method        $this               ceilMinute(float $precision = 1)                                                     Ceil the current instance minute with given precision.
 * @method        $this               ceilMinutes(float $precision = 1)                                                    Ceil the current instance minute with given precision.

Basic testing shows that given input "2021-11-26 11:25:10.000000", both functions round it up to "2021-11-26 11:26:00.000000".

And I want to know is there any difference between these two functions?

Any help would be appreciated.


Solution

  • Short answer: they're 100% equivalent.

    Carbon implements almost everything using magic methods. The implementation of __call() basically strips trailing s:

    /**
     * Dynamically handle calls to the class.
     *
     * @param string $method     magic method name called
     * @param array  $parameters parameters list
     *
     * @throws UnknownMethodException|BadMethodCallException|ReflectionException|Throwable
     *
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        // [...]
        $unit = rtrim($method, 's');
    

    ... and uses that to fetch a value from the units list:

        if (\in_array($unit, static::$units)) {
            return $this->setUnit($unit, ...$parameters);
        }
    
    protected static $units = [
        // @call setUnit
        // @call addUnit
        'year',
        // @call setUnit
        // @call addUnit
        'month',
        // @call setUnit
        // @call addUnit
        'day',
        // @call setUnit
        // @call addUnit
        'hour',
        // @call setUnit
        // @call addUnit
        'minute',
        // @call setUnit
        // @call addUnit
        'second',
        // @call setUnit
        // @call addUnit
        'milli',
        // @call setUnit
        // @call addUnit
        'millisecond',
        // @call setUnit
        // @call addUnit
        'micro',
        // @call setUnit
        // @call addUnit
        'microsecond',
    ];