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.
示例#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;
     }
 }
 /**
  * Test timezones with input date and time in other timezones
  * @dataProvider provideTimesAndTz
  */
 public function testTimezoneInput($defaultTz, $inputTimeDst, $inputTimeNonDst)
 {
     date_default_timezone_set($defaultTz);
     // formatting has to be independent of the default timezone set by PHP
     $this->formatter->datetimeFormat = 'yyyy-MM-dd HH:mm:ss';
     $this->formatter->dateFormat = 'yyyy-MM-dd';
     $this->formatter->timeFormat = 'HH:mm:ss';
     // daylight saving time
     $this->formatter->timeZone = 'UTC';
     $this->assertSame('2014-08-10 12:41:00', $this->formatter->asDatetime($inputTimeDst));
     $this->assertSame('2014-08-10', $this->formatter->asDate($inputTimeDst));
     $this->assertSame('12:41:00', $this->formatter->asTime($inputTimeDst));
     $this->assertSame('1407674460', $this->formatter->asTimestamp($inputTimeDst));
     $this->formatter->timeZone = 'Europe/Berlin';
     $this->assertSame('2014-08-10 14:41:00', $this->formatter->asDatetime($inputTimeDst));
     $this->assertSame('2014-08-10', $this->formatter->asDate($inputTimeDst));
     $this->assertSame('14:41:00', $this->formatter->asTime($inputTimeDst));
     $this->assertSame('1407674460', $this->formatter->asTimestamp($inputTimeDst));
     // non daylight saving time
     $this->formatter->timeZone = 'UTC';
     $this->assertSame('2014-01-01 12:41:00', $this->formatter->asDatetime($inputTimeNonDst));
     $this->assertSame('2014-01-01', $this->formatter->asDate($inputTimeNonDst));
     $this->assertSame('12:41:00', $this->formatter->asTime($inputTimeNonDst));
     $this->assertSame('1388580060', $this->formatter->asTimestamp($inputTimeNonDst));
     $this->formatter->timeZone = 'Europe/Berlin';
     $this->assertSame('2014-01-01 13:41:00', $this->formatter->asDatetime($inputTimeNonDst));
     $this->assertSame('2014-01-01', $this->formatter->asDate($inputTimeNonDst));
     $this->assertSame('13:41:00', $this->formatter->asTime($inputTimeNonDst));
     $this->assertSame('1388580060', $this->formatter->asTimestamp($inputTimeNonDst));
     // tests for relative time
     if ($inputTimeDst !== 1407674460 && !is_object($inputTimeDst)) {
         $this->assertSame('3 hours ago', $this->formatter->asRelativeTime($inputTimeDst, $relativeTime = str_replace(['14:41', '12:41'], ['17:41', '15:41'], $inputTimeDst)));
         $this->assertSame('in 3 hours', $this->formatter->asRelativeTime($relativeTime, $inputTimeDst));
         $this->assertSame('3 hours ago', $this->formatter->asRelativeTime($inputTimeNonDst, $relativeTime = str_replace(['13:41', '12:41'], ['16:41', '15:41'], $inputTimeNonDst)));
         $this->assertSame('in 3 hours', $this->formatter->asRelativeTime($relativeTime, $inputTimeNonDst));
     }
 }
示例#3
0
 public function testAsRelativeTime()
 {
     $interval_1_second = new DateInterval("PT1S");
     $interval_244_seconds = new DateInterval("PT244S");
     $interval_1_minute = new DateInterval("PT1M");
     $interval_33_minutes = new DateInterval("PT33M");
     $interval_1_hour = new DateInterval("PT1H");
     $interval_6_hours = new DateInterval("PT6H");
     $interval_1_day = new DateInterval("P1D");
     $interval_89_days = new DateInterval("P89D");
     $interval_1_month = new DateInterval("P1M");
     $interval_5_months = new DateInterval("P5M");
     $interval_1_year = new DateInterval("P1Y");
     $interval_12_years = new DateInterval("P12Y");
     // Pass a DateInterval
     $this->assertSame('a second ago', $this->formatter->asRelativeTime($interval_1_second));
     $this->assertSame('244 seconds ago', $this->formatter->asRelativeTime($interval_244_seconds));
     $this->assertSame('a minute ago', $this->formatter->asRelativeTime($interval_1_minute));
     $this->assertSame('33 minutes ago', $this->formatter->asRelativeTime($interval_33_minutes));
     $this->assertSame('an hour ago', $this->formatter->asRelativeTime($interval_1_hour));
     $this->assertSame('6 hours ago', $this->formatter->asRelativeTime($interval_6_hours));
     $this->assertSame('a day ago', $this->formatter->asRelativeTime($interval_1_day));
     $this->assertSame('89 days ago', $this->formatter->asRelativeTime($interval_89_days));
     $this->assertSame('a month ago', $this->formatter->asRelativeTime($interval_1_month));
     $this->assertSame('5 months ago', $this->formatter->asRelativeTime($interval_5_months));
     $this->assertSame('a year ago', $this->formatter->asRelativeTime($interval_1_year));
     $this->assertSame('12 years ago', $this->formatter->asRelativeTime($interval_12_years));
     // Pass a DateInterval string -> isn't possible
     //    $this->assertSame('a year ago', $this->formatter->asRelativeTime('2007-03-01T13:00:00Z/2008-05-11T15:30:00Z'));
     //    $this->assertSame('a year ago', $this->formatter->asRelativeTime('2007-03-01T13:00:00Z/P1Y2M10DT2H30M'));
     //    $this->assertSame('a year ago', $this->formatter->asRelativeTime('P1Y2M10DT2H30M/2008-05-11T15:30:00Z'));
     //    $this->assertSame('a year ago', $this->formatter->asRelativeTime('P1Y2M10DT2H30M'));
     //    $this->assertSame('94 months ago', $this->formatter->asRelativeTime('P94M'));
     // Force the reference time and pass a past DateTime
     $dateNow = new DateTime('2014-03-13');
     $this->assertSame('a second ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_second]), $dateNow));
     $this->assertSame('4 minutes ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_244_seconds]), $dateNow));
     $this->assertSame('a minute ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_minute]), $dateNow));
     $this->assertSame('33 minutes ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_33_minutes]), $dateNow));
     $this->assertSame('an hour ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_hour]), $dateNow));
     $this->assertSame('6 hours ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_6_hours]), $dateNow));
     $this->assertSame('a day ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_day]), $dateNow));
     $this->assertSame('2 months ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_89_days]), $dateNow));
     $this->assertSame('a month ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_month]), $dateNow));
     $this->assertSame('5 months ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_5_months]), $dateNow));
     $this->assertSame('a year ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_year]), $dateNow));
     $this->assertSame('12 years ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_12_years]), $dateNow));
     // Tricky 31-days month stuff
     // See: http://www.gnu.org/software/tar/manual/html_section/Relative-items-in-date-strings.html
     $dateNow = new DateTime('2014-03-31');
     $dateThen = new DateTime('2014-03-03');
     $this->assertSame('28 days ago', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-31', [$interval_1_month]), $dateNow));
     $this->assertSame('28 days ago', $this->formatter->asRelativeTime($dateThen, $dateNow));
     $dateThen = new DateTime('2014-02-28');
     $this->assertSame('a month ago', $this->formatter->asRelativeTime($dateThen, $dateNow));
     // Invert all the DateIntervals
     $interval_1_second->invert = true;
     $interval_244_seconds->invert = true;
     $interval_1_minute->invert = true;
     $interval_33_minutes->invert = true;
     $interval_1_hour->invert = true;
     $interval_6_hours->invert = true;
     $interval_1_day->invert = true;
     $interval_89_days->invert = true;
     $interval_1_month->invert = true;
     $interval_5_months->invert = true;
     $interval_1_year->invert = true;
     $interval_12_years->invert = true;
     // Pass a inverted DateInterval
     $this->assertSame('in a second', $this->formatter->asRelativeTime($interval_1_second));
     $this->assertSame('in 244 seconds', $this->formatter->asRelativeTime($interval_244_seconds));
     $this->assertSame('in a minute', $this->formatter->asRelativeTime($interval_1_minute));
     $this->assertSame('in 33 minutes', $this->formatter->asRelativeTime($interval_33_minutes));
     $this->assertSame('in an hour', $this->formatter->asRelativeTime($interval_1_hour));
     $this->assertSame('in 6 hours', $this->formatter->asRelativeTime($interval_6_hours));
     $this->assertSame('in a day', $this->formatter->asRelativeTime($interval_1_day));
     $this->assertSame('in 89 days', $this->formatter->asRelativeTime($interval_89_days));
     $this->assertSame('in a month', $this->formatter->asRelativeTime($interval_1_month));
     $this->assertSame('in 5 months', $this->formatter->asRelativeTime($interval_5_months));
     $this->assertSame('in a year', $this->formatter->asRelativeTime($interval_1_year));
     $this->assertSame('in 12 years', $this->formatter->asRelativeTime($interval_12_years));
     // Pass a inverted DateInterval string
     // $this->assertSame('in a year', $this->formatter->asRelativeTime('2008-05-11T15:30:00Z/2007-03-01T13:00:00Z'));
     // Force the reference time and pass a future DateTime
     $dateNow = new DateTime('2014-03-13');
     $this->assertSame('in a second', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_second]), $dateNow));
     $this->assertSame('in 4 minutes', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_244_seconds]), $dateNow));
     $this->assertSame('in a minute', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_minute]), $dateNow));
     $this->assertSame('in 33 minutes', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_33_minutes]), $dateNow));
     $this->assertSame('in an hour', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_hour]), $dateNow));
     $this->assertSame('in 6 hours', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_6_hours]), $dateNow));
     $this->assertSame('in a day', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_day]), $dateNow));
     $this->assertSame('in 2 months', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_89_days]), $dateNow));
     $this->assertSame('in a month', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_month]), $dateNow));
     $this->assertSame('in 5 months', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_5_months]), $dateNow));
     $this->assertSame('in a year', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_1_year]), $dateNow));
     $this->assertSame('in 12 years', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-13', [$interval_12_years]), $dateNow));
     // Tricky 31-days month stuff
     // See: http://www.gnu.org/software/tar/manual/html_section/Relative-items-in-date-strings.html
     $dateNow = new DateTime('2014-03-03');
     $dateThen = new DateTime('2014-03-31');
     $this->assertSame('in a month', $this->formatter->asRelativeTime($this->buildDateSubIntervals('2014-03-03', [$interval_1_month]), $dateNow));
     $this->assertSame('in 28 days', $this->formatter->asRelativeTime($dateThen, $dateNow));
     // just now
     $this->assertSame("just now", $this->formatter->asRelativeTime($t = time(), $t));
     $this->assertSame("just now", $this->formatter->asRelativeTime(0, 0));
     // empty input
     $this->assertSame("just now", $this->formatter->asRelativeTime(false, 0));
     $this->assertSame("just now", $this->formatter->asRelativeTime("", 0));
     // null display
     $this->assertSame($this->formatter->nullDisplay, $this->formatter->asRelativeTime(null));
     $this->assertSame($this->formatter->nullDisplay, $this->formatter->asRelativeTime(null, time()));
 }