yii\i18n\Formatter::asRelativeTime PHP Method

asRelativeTime() public method

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.
public asRelativeTime ( integer | string | DateTim\DateTime | DateInterva\DateInterval $value, integer | string | DateTim\DateTime $referenceTime = null ) : string
$value integer | string | DateTim\DateTime | DateInterva\DateInterval 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](http://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](http://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)
$referenceTime integer | string | DateTim\DateTime 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.
    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);
        } else {
            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);
        }
    }

Usage Example

Beispiel #1
0
 /**
  * The same as asRelativeTime, but with overdue highlighting
  * @param $value
  * @param null $referenceTime
  * @return string
  */
 public function asRelativeTimeHighlight($value, $referenceTime = null)
 {
     if ($value === date('Y-m-d')) {
         return Html::tag('span', \Yii::t('app', 'today'), ['class' => 'text-warning']);
     } else {
         $sOut = parent::asRelativeTime($value, $referenceTime);
         if ($value < date('Y-m-d')) {
             $sOut = Html::tag('span', $sOut, ['class' => 'text-danger']);
         }
         return $sOut;
     }
 }
All Usage Examples Of yii\i18n\Formatter::asRelativeTime