src/Charcoal/Admin/Template/System/ClearCacheTemplate.php
private function formatTimeDiff(DateTimeInterface $date1, DateTimeInterface $date2 = null)
    {
        $isNow = $date2 === null;
        if ($isNow) {
            $date2 = new DateTime('now', $date1->getTimezone());
        }
        $interval = $date1->diff($date2);

        $translator = $this->translator();

        switch (true) {
            case ($interval->y > 0):
                $unit  = 'time.year';
                $count = $interval->y;
                break;
            case ($interval->m > 0):
                $unit  = 'time.month';
                $count = $interval->m;
                break;
            case ($interval->d > 0):
                $unit  = 'time.day';
                $count = $interval->d;
                if ($count >= 7) {
                    $unit  = 'time.week';
                    $count = (int)($count / 7);
                }
                break;
            case ($interval->h > 0):
                $unit  = 'time.hour';
                $count = $interval->h;
                break;
            case ($interval->i > 0):
                $unit  = 'time.minute';
                $count = $interval->i;
                break;
            default:
                $count = $interval->s;
                $unit  = 'time.second';
                break;
        }

        $time = $translator->transChoice($unit, $count, [ '{{ count }}' => $count ]);

        return $time;
    }
private function formatTimeDiff(DateTimeInterface $date1, DateTimeInterface $date2 = null)
    {
        $isNow = $date2 === null;
        if ($isNow) {
            $date2 = new DateTime('now', $date1->getTimezone());
        }
        $interval = $date1->diff($date2);

        $translator = $this->translator();

        switch (true) {
            case ($interval->y > 0):
                $unit  = 'time.year';
                $count = $interval->y;
                break;
            case ($interval->m > 0):
                $unit  = 'time.month';
                $count = $interval->m;
                break;
            case ($interval->d > 0):
                $unit  = 'time.day';
                $count = $interval->d;
                if ($count >= 7) {
                    $unit  = 'time.week';
                    $count = (int)($count / 7);
                }
                break;
            case ($interval->h > 0):
                $unit  = 'time.hour';
                $count = $interval->h;
                break;
            case ($interval->i > 0):
                $unit  = 'time.minute';
                $count = $interval->i;
                break;
            default:
                $count = $interval->s;
                $unit  = 'time.second';
                break;
        }

        $time = $translator->transChoice($unit, $count, [ '{{ count }}' => $count ]);

        return $time;
    }
private function formatTimeDiff ( DateTimeInterface $ date1 , DateTimeInterface $ date2 = null ) { $ isNow = $ date2 === null ; if ( $ isNow ) { $ date2 = new DateTime ( 'now' , $ date1 -> getTimezone ( ) ) ; } $ interval = $ date1 -> diff ( $ date2 ) ; $ translator = $ this -> translator ( ) ; switch ( true ) { case ( $ interval -> y > 0 ) : $ unit = 'time.year' ; $ count = $ interval -> y ; break ; case ( $ interval -> m > 0 ) : $ unit = 'time.month' ; $ count = $ interval -> m ; break ; case ( $ interval -> d > 0 ) : $ unit = 'time.day' ; $ count = $ interval -> d ; if ( $ count >= 7 ) { $ unit = 'time.week' ; $ count = ( int ) ( $ count / 7 ) ; } break ; case ( $ interval -> h > 0 ) : $ unit = 'time.hour' ; $ count = $ interval -> h ; break ; case ( $ interval -> i > 0 ) : $ unit = 'time.minute' ; $ count = $ interval -> i ; break ; default : $ count = $ interval -> s ; $ unit = 'time.second' ; break ; } $ time = $ translator -> transChoice ( $ unit , $ count , [ '{{ count }}' => $ count ] ) ; return $ time ; }
Human-readable time difference.

Note: Adapted from CakePHP\Chronos.

@see https://github.com/cakephp/chronos/blob/1.1.4/LICENSE

@param DateTimeInterface      $date1 The datetime to start with.
@param DateTimeInterface|null $date2 The datetime to compare against.
@return string
Human - readable time difference .