framework/i18n/Formatter.php
public function asRelativeTime($value, $referenceTime = null)
    {
        if ($value === null) {
            return $this->nullDisplay;
        }

        if ($value instanceof DateInterval) {
            $interval = $value;
        } else {
            $timestamp = $this->normalizeDatetimeValue($value);

            if ($timestamp === false) {
                // $value is not a valid date/time value, so we try
                // to create a DateInterval with it
                try {
                    $interval = new DateInterval($value);
                } catch (\Exception $e) {
                    // invalid date/time and invalid interval
                    return $this->nullDisplay;
                }
            } else {
                $timeZone = new DateTimeZone($this->timeZone);

                if ($referenceTime === null) {
                    $dateNow = new DateTime('now', $timeZone);
                } else {
                    $dateNow = $this->normalizeDatetimeValue($referenceTime);
                    $dateNow->setTimezone($timeZone);
                }

                $dateThen = $timestamp->setTimezone($timeZone);

                $interval = $dateThen->diff($dateNow);
            }
        }

        if ($interval->invert) {
            if ($interval->y >= 1) {
                return Yii::t('yii', 'in {delta, plural, =1{a year} other{# years}}', ['delta' => $interval->y], $this->locale);
            }
            if ($interval->m >= 1) {
                return Yii::t('yii', 'in {delta, plural, =1{a month} other{# months}}', ['delta' => $interval->m], $this->locale);
            }
            if ($interval->d >= 1) {
                return Yii::t('yii', 'in {delta, plural, =1{a day} other{# days}}', ['delta' => $interval->d], $this->locale);
            }
            if ($interval->h >= 1) {
                return Yii::t('yii', 'in {delta, plural, =1{an hour} other{# hours}}', ['delta' => $interval->h], $this->locale);
            }
            if ($interval->i >= 1) {
                return Yii::t('yii', 'in {delta, plural, =1{a minute} other{# minutes}}', ['delta' => $interval->i], $this->locale);
            }
            if ($interval->s == 0) {
                return Yii::t('yii', 'just now', [], $this->locale);
            }

            return Yii::t('yii', 'in {delta, plural, =1{a second} other{# seconds}}', ['delta' => $interval->s], $this->locale);
        }

        if ($interval->y >= 1) {
            return Yii::t('yii', '{delta, plural, =1{a year} other{# years}} ago', ['delta' => $interval->y], $this->locale);
        }
        if ($interval->m >= 1) {
            return Yii::t('yii', '{delta, plural, =1{a month} other{# months}} ago', ['delta' => $interval->m], $this->locale);
        }
        if ($interval->d >= 1) {
            return Yii::t('yii', '{delta, plural, =1{a day} other{# days}} ago', ['delta' => $interval->d], $this->locale);
        }
        if ($interval->h >= 1) {
            return Yii::t('yii', '{delta, plural, =1{an hour} other{# hours}} ago', ['delta' => $interval->h], $this->locale);
        }
        if ($interval->i >= 1) {
            return Yii::t('yii', '{delta, plural, =1{a minute} other{# minutes}} ago', ['delta' => $interval->i], $this->locale);
        }
        if ($interval->s == 0) {
            return Yii::t('yii', 'just now', [], $this->locale);
        }

        return Yii::t('yii', '{delta, plural, =1{a second} other{# seconds}} ago', ['delta' => $interval->s], $this->locale);
    }
public function asRelativeTime($value, $referenceTime = null)
    {
        if ($value === null) {
            return $this->nullDisplay;
        }

        if ($value instanceof DateInterval) {
            $interval = $value;
        } else {
            $timestamp = $this->normalizeDatetimeValue($value);

            if ($timestamp === false) {
                // $value is not a valid date/time value, so we try
                // to create a DateInterval with it
                try {
                    $interval = new DateInterval($value);
                } catch (\Exception $e) {
                    // invalid date/time and invalid interval
                    return $this->nullDisplay;
                }
            } else {
                $timeZone = new DateTimeZone($this->timeZone);

                if ($referenceTime === null) {
                    $dateNow = new DateTime('now', $timeZone);
                } else {
                    $dateNow = $this->normalizeDatetimeValue($referenceTime);
                    $dateNow->setTimezone($timeZone);
                }

                $dateThen = $timestamp->setTimezone($timeZone);

                $interval = $dateThen->diff($dateNow);
            }
        }

        if ($interval->invert) {
            if ($interval->y >= 1) {
                return Yii::t('yii', 'in {delta, plural, =1{a year} other{# years}}', ['delta' => $interval->y], $this->locale);
            }
            if ($interval->m >= 1) {
                return Yii::t('yii', 'in {delta, plural, =1{a month} other{# months}}', ['delta' => $interval->m], $this->locale);
            }
            if ($interval->d >= 1) {
                return Yii::t('yii', 'in {delta, plural, =1{a day} other{# days}}', ['delta' => $interval->d], $this->locale);
            }
            if ($interval->h >= 1) {
                return Yii::t('yii', 'in {delta, plural, =1{an hour} other{# hours}}', ['delta' => $interval->h], $this->locale);
            }
            if ($interval->i >= 1) {
                return Yii::t('yii', 'in {delta, plural, =1{a minute} other{# minutes}}', ['delta' => $interval->i], $this->locale);
            }
            if ($interval->s == 0) {
                return Yii::t('yii', 'just now', [], $this->locale);
            }

            return Yii::t('yii', 'in {delta, plural, =1{a second} other{# seconds}}', ['delta' => $interval->s], $this->locale);
        }

        if ($interval->y >= 1) {
            return Yii::t('yii', '{delta, plural, =1{a year} other{# years}} ago', ['delta' => $interval->y], $this->locale);
        }
        if ($interval->m >= 1) {
            return Yii::t('yii', '{delta, plural, =1{a month} other{# months}} ago', ['delta' => $interval->m], $this->locale);
        }
        if ($interval->d >= 1) {
            return Yii::t('yii', '{delta, plural, =1{a day} other{# days}} ago', ['delta' => $interval->d], $this->locale);
        }
        if ($interval->h >= 1) {
            return Yii::t('yii', '{delta, plural, =1{an hour} other{# hours}} ago', ['delta' => $interval->h], $this->locale);
        }
        if ($interval->i >= 1) {
            return Yii::t('yii', '{delta, plural, =1{a minute} other{# minutes}} ago', ['delta' => $interval->i], $this->locale);
        }
        if ($interval->s == 0) {
            return Yii::t('yii', 'just now', [], $this->locale);
        }

        return Yii::t('yii', '{delta, plural, =1{a second} other{# seconds}} ago', ['delta' => $interval->s], $this->locale);
    }
public function asRelativeTime ( $ value , $ referenceTime = null ) { if ( $ value === null ) { return $ this -> nullDisplay ; } if ( $ value instanceof DateInterval ) { $ interval = $ value ; } else { $ timestamp = $ this -> normalizeDatetimeValue ( $ value ) ; if ( $ timestamp === false ) { // $value is not a valid date/time value, so we try // to create a DateInterval with it try { $ interval = new DateInterval ( $ value ) ; } catch ( \ Exception $ e ) { // invalid date/time and invalid interval return $ this -> nullDisplay ; } } else { $ timeZone = new DateTimeZone ( $ this -> timeZone ) ; if ( $ referenceTime === null ) { $ dateNow = new DateTime ( 'now' , $ timeZone ) ; } else { $ dateNow = $ this -> normalizeDatetimeValue ( $ referenceTime ) ; $ dateNow -> setTimezone ( $ timeZone ) ; } $ dateThen = $ timestamp -> setTimezone ( $ timeZone ) ; $ interval = $ dateThen -> diff ( $ dateNow ) ; } } if ( $ interval -> invert ) { if ( $ interval -> y >= 1 ) { return Yii :: t ( 'yii' , 'in {delta, plural, =1{a year} other{# years}}' , [ 'delta' => $ interval -> y ] , $ this -> locale ) ; } if ( $ interval -> m >= 1 ) { return Yii :: t ( 'yii' , 'in {delta, plural, =1{a month} other{# months}}' , [ 'delta' => $ interval -> m ] , $ this -> locale ) ; } if ( $ interval -> d >= 1 ) { return Yii :: t ( 'yii' , 'in {delta, plural, =1{a day} other{# days}}' , [ 'delta' => $ interval -> d ] , $ this -> locale ) ; } if ( $ interval -> h >= 1 ) { return Yii :: t ( 'yii' , 'in {delta, plural, =1{an hour} other{# hours}}' , [ 'delta' => $ interval -> h ] , $ this -> locale ) ; } if ( $ interval -> i >= 1 ) { return Yii :: t ( 'yii' , 'in {delta, plural, =1{a minute} other{# minutes}}' , [ 'delta' => $ interval -> i ] , $ this -> locale ) ; } if ( $ interval -> s == 0 ) { return Yii :: t ( 'yii' , 'just now' , [ ] , $ this -> locale ) ; } return Yii :: t ( 'yii' , 'in {delta, plural, =1{a second} other{# seconds}}' , [ 'delta' => $ interval -> s ] , $ this -> locale ) ; } if ( $ interval -> y >= 1 ) { return Yii :: t ( 'yii' , '{delta, plural, =1{a year} other{# years}} ago' , [ 'delta' => $ interval -> y ] , $ this -> locale ) ; } if ( $ interval -> m >= 1 ) { return Yii :: t ( 'yii' , '{delta, plural, =1{a month} other{# months}} ago' , [ 'delta' => $ interval -> m ] , $ this -> locale ) ; } if ( $ interval -> d >= 1 ) { return Yii :: t ( 'yii' , '{delta, plural, =1{a day} other{# days}} ago' , [ 'delta' => $ interval -> d ] , $ this -> locale ) ; } if ( $ interval -> h >= 1 ) { return Yii :: t ( 'yii' , '{delta, plural, =1{an hour} other{# hours}} ago' , [ 'delta' => $ interval -> h ] , $ this -> locale ) ; } if ( $ interval -> i >= 1 ) { return Yii :: t ( 'yii' , '{delta, plural, =1{a minute} other{# minutes}} ago' , [ 'delta' => $ interval -> i ] , $ this -> locale ) ; } if ( $ interval -> s == 0 ) { return Yii :: t ( 'yii' , 'just now' , [ ] , $ this -> locale ) ; } return Yii :: t ( 'yii' , '{delta, plural, =1{a second} other{# seconds}} ago' , [ 'delta' => $ interval -> s ] , $ this -> locale ) ; }
Formats the value as the time interval between a date and now in human readable form.

This method can be used in three different ways:

1. Using a timestamp that is relative to `now`.
2. Using a timestamp that is relative to the `$referenceTime`.
3. Using a `DateInterval` object.

@param int|string|DateTime|DateInterval $value the value to be formatted. The following
types of value are supported:

- an integer representing a UNIX timestamp
- a string that can be [parsed to create a DateTime object](https://secure.php.net/manual/en/datetime.formats.php).
The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given.
- a PHP [DateTime](https://secure.php.net/manual/en/class.datetime.php) object
- a PHP DateInterval object (a positive time interval will refer to the past, a negative one to the future)

@param int|string|DateTime $referenceTime if specified the value is used as a reference time instead of `now`
when `$value` is not a `DateInterval` object.
@return string the formatted result.
@throws InvalidArgumentException if the input value can not be evaluated as a date value.
Formats the value as the time interval between a date and now in human readable form .