Cake\Chronos\DifferenceFormatter::diffForHumans PHP Method

diffForHumans() public method

Get the difference in a human readable format.
See also: Cake\Chronos\ChronosInterface::diffForHumans
public diffForHumans ( Cake\Chronos\ChronosInterface $date, Cake\Chronos\ChronosInterface $other = null, boolean $absolute = false ) : string
$date Cake\Chronos\ChronosInterface The datetime to start with.
$other Cake\Chronos\ChronosInterface The datetime to compare against.
$absolute boolean removes time difference modifiers ago, after, etc
return string The difference between the two days in a human readable format
    public function diffForHumans(ChronosInterface $date, ChronosInterface $other = null, $absolute = false)
    {
        $isNow = $other === null;
        if ($isNow) {
            $other = $date->now($date->tz);
        }
        $diffInterval = $date->diff($other);
        switch (true) {
            case $diffInterval->y > 0:
                $unit = 'year';
                $count = $diffInterval->y;
                break;
            case $diffInterval->m > 0:
                $unit = 'month';
                $count = $diffInterval->m;
                break;
            case $diffInterval->d > 0:
                $unit = 'day';
                $count = $diffInterval->d;
                if ($count >= ChronosInterface::DAYS_PER_WEEK) {
                    $unit = 'week';
                    $count = (int) ($count / ChronosInterface::DAYS_PER_WEEK);
                }
                break;
            case $diffInterval->h > 0:
                $unit = 'hour';
                $count = $diffInterval->h;
                break;
            case $diffInterval->i > 0:
                $unit = 'minute';
                $count = $diffInterval->i;
                break;
            default:
                $count = $diffInterval->s;
                $unit = 'second';
                break;
        }
        if ($count === 0) {
            $count = 1;
        }
        $time = $this->translate->plural($unit, $count, ['count' => $count]);
        if ($absolute) {
            return $time;
        }
        $isFuture = $diffInterval->invert === 1;
        $transId = $isNow ? $isFuture ? 'from_now' : 'ago' : ($isFuture ? 'after' : 'before');
        // Some langs have special pluralization for past and future tense.
        $tryKeyExists = $unit . '_' . $transId;
        if ($this->translate->exists($tryKeyExists)) {
            $time = $this->translate->plural($tryKeyExists, $count, ['count' => $count]);
        }
        return $this->translate->singular($transId, ['time' => $time]);
    }
DifferenceFormatter