Jyxo\Time\Time::formatAsInterval PHP Method

formatAsInterval() public method

$t < 10 seconds = Now 10 seconds <= $t < 60 seconds 1 minute <= $t < 1 hour 1 hour <= $t < 24 hours 1 day <= $t < 7 days 1 week <= $t < 4 weeks 1 month <= $t < 12 months 1 year <= $t < n years
public formatAsInterval ( boolean $useTense = true, string | DateTimeZone $timeZone = null ) : string
$useTense boolean Defines if declension should be used
$timeZone string | DateTimeZone Result time zone definition
return string
    public function formatAsInterval(bool $useTense = true, $timeZone = null) : string
    {
        static $intervalList = [self::YEAR => self::INTERVAL_YEAR, self::MONTH => self::INTERVAL_MONTH, self::WEEK => self::INTERVAL_WEEK, self::DAY => self::INTERVAL_DAY, self::HOUR => self::INTERVAL_HOUR, self::MINUTE => self::INTERVAL_MINUTE, self::SECOND => self::INTERVAL_SECOND];
        // Comparison time zone
        $timeZone = $timeZone ? $this->createTimeZone($timeZone) : $this->dateTime->getTimezone();
        // Difference between the stored date/time and now
        $differenceObject = $this->dateTime->diff(new \DateTime('', $timeZone));
        $diffArray = array_combine(array_keys($intervalList), explode('-', $differenceObject->format('%y-%m-0-%d-%h-%i-%s')));
        // Compute the difference in seconds
        $diff = 0;
        foreach ($diffArray as $interval => $intervalCount) {
            $diff += $intervalList[$interval] * $intervalCount;
        }
        // If the difference is less than 10 seconds, "now" is returned
        if ($diff < 10) {
            return _('Now');
        }
        // Find the appropriate unit and calculate number of units
        foreach ($intervalList as $interval => $seconds) {
            if ($seconds <= $diff) {
                $num = (int) round($diff / $seconds);
                break;
            }
        }
        // Past or future
        $period = '+' === $differenceObject->format('%R') ? 'past' : 'future';
        // Dictionary - this part could be written shorter but this implementation is faster
        $tense = $useTense ? $period : 'infinitive';
        switch ($tense) {
            // Past
            case 'past':
                switch ($interval) {
                    case self::YEAR:
                        return sprintf(ngettext('Year ago', '%s years ago', $num), $num);
                    case self::MONTH:
                        return sprintf(ngettext('Month ago', '%s months ago', $num), $num);
                    case self::WEEK:
                        return sprintf(ngettext('Week ago', '%s weeks ago', $num), $num);
                    case self::DAY:
                        return sprintf(ngettext('Day ago', '%s days ago', $num), $num);
                    case self::HOUR:
                        return sprintf(ngettext('Hour ago', '%s hours ago', $num), $num);
                    case self::MINUTE:
                        return sprintf(ngettext('Minute ago', '%s minutes ago', $num), $num);
                    case self::SECOND:
                    default:
                        return sprintf(ngettext('Second ago', '%s seconds ago', $num), $num);
                }
                break;
                // Future
            // Future
            case 'future':
                switch ($interval) {
                    case self::YEAR:
                        return sprintf(ngettext('In year', 'In %s years', $num), $num);
                    case self::MONTH:
                        return sprintf(ngettext('In month', 'In %s months', $num), $num);
                    case self::WEEK:
                        return sprintf(ngettext('In week', 'In %s weeks', $num), $num);
                    case self::DAY:
                        return sprintf(ngettext('In day', 'In %s days', $num), $num);
                    case self::HOUR:
                        return sprintf(ngettext('In hour', 'In %s hours', $num), $num);
                    case self::MINUTE:
                        return sprintf(ngettext('In minute', 'In %s minutes', $num), $num);
                    case self::SECOND:
                    default:
                        return sprintf(ngettext('In second', 'In %s seconds', $num), $num);
                }
                break;
                // Infinitive
            // Infinitive
            case 'infinitive':
                switch ($interval) {
                    case self::YEAR:
                        return sprintf(ngettext('Year', '%s years', $num), $num);
                    case self::MONTH:
                        return sprintf(ngettext('Month', '%s months', $num), $num);
                    case self::WEEK:
                        return sprintf(ngettext('Week', '%s weeks', $num), $num);
                    case self::DAY:
                        return sprintf(ngettext('Day', '%s days', $num), $num);
                    case self::HOUR:
                        return sprintf(ngettext('Hour', '%s hours', $num), $num);
                    case self::MINUTE:
                        return sprintf(ngettext('Minute', '%s minutes', $num), $num);
                    case self::SECOND:
                    default:
                        return sprintf(ngettext('Second', '%s seconds', $num), $num);
                }
                break;
            default:
                break;
        }
    }