Formats the value as a time.
public asTime ( integer | string | DateTim\DateTime $value, string $format = null ) : string | ||
$value | integer | string | DateTim\DateTime | 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 |
$format | string | the format used to convert the value into a date string. If null, [[timeFormat]] will be used. This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. It can also be a custom format as specified in the [ICU manual](http://userguide.icu-project.org/formatparse/datetime). Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the PHP [date()](http://php.net/manual/en/function.date.php)-function. |
Résultat | string | the formatted result. |
public function testAsTime() { $value = time(); $this->assertSame(date('g:i:s A', $value), $this->formatter->asTime($value)); $this->assertSame(date('h:i:s A', $value), $this->formatter->asTime($value, 'php:h:i:s A')); // null display $this->assertSame($this->formatter->nullDisplay, $this->formatter->asTime(null)); }
public function testAsTime() { $value = time(); $this->assertSame(date('g:i:s A', $value), $this->formatter->asTime($value)); $this->assertSame(date('h:i:s A', $value), $this->formatter->asTime($value, 'php:h:i:s A')); // empty input $this->assertSame('12:00:00 AM', $this->formatter->asTime('')); $this->assertSame('12:00:00 AM', $this->formatter->asTime(0)); $this->assertSame('12:00:00 AM', $this->formatter->asTime(false)); // null display $this->assertSame($this->formatter->nullDisplay, $this->formatter->asTime(null)); }
/** * 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) { $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)); } }
/** * Test timezones with input date and time in other timezones */ public function testTimezoneInputNonDefault() { $this->formatter->datetimeFormat = 'yyyy-MM-dd HH:mm:ss'; $this->formatter->dateFormat = 'yyyy-MM-dd'; $this->formatter->timeFormat = 'HH:mm:ss'; $this->formatter->timeZone = 'UTC'; $this->formatter->defaultTimeZone = 'UTC'; $this->assertSame('2014-08-10 12:41:00', $this->formatter->asDatetime('2014-08-10 12:41:00')); $this->assertSame('2014-08-10', $this->formatter->asDate('2014-08-10 12:41:00')); $this->assertSame('12:41:00', $this->formatter->asTime('2014-08-10 12:41:00')); $this->assertSame('1407674460', $this->formatter->asTimestamp('2014-08-10 12:41:00')); $this->assertSame('2014-08-10 10:41:00', $this->formatter->asDatetime('2014-08-10 12:41:00 Europe/Berlin')); $this->assertSame('2014-08-10', $this->formatter->asDate('2014-08-10 12:41:00 Europe/Berlin')); $this->assertSame('10:41:00', $this->formatter->asTime('2014-08-10 12:41:00 Europe/Berlin')); $this->assertSame('1407674460', $this->formatter->asTimestamp('2014-08-10 14:41:00 Europe/Berlin')); $this->formatter->timeZone = 'Europe/Berlin'; $this->formatter->defaultTimeZone = 'Europe/Berlin'; $this->assertSame('2014-08-10 12:41:00', $this->formatter->asDatetime('2014-08-10 12:41:00')); $this->assertSame('2014-08-10', $this->formatter->asDate('2014-08-10 12:41:00')); $this->assertSame('12:41:00', $this->formatter->asTime('2014-08-10 12:41:00')); $this->assertSame('1407674460', $this->formatter->asTimestamp('2014-08-10 14:41:00')); $this->assertSame('2014-08-10 12:41:00', $this->formatter->asDatetime('2014-08-10 12:41:00 Europe/Berlin')); $this->assertSame('2014-08-10', $this->formatter->asDate('2014-08-10 12:41:00 Europe/Berlin')); $this->assertSame('12:41:00', $this->formatter->asTime('2014-08-10 12:41:00 Europe/Berlin')); $this->assertSame('1407674460', $this->formatter->asTimestamp('2014-08-10 14:41:00 Europe/Berlin')); $this->formatter->timeZone = 'UTC'; $this->formatter->defaultTimeZone = 'Europe/Berlin'; $this->assertSame('2014-08-10 10:41:00', $this->formatter->asDatetime('2014-08-10 12:41:00')); $this->assertSame('2014-08-10', $this->formatter->asDate('2014-08-10 12:41:00')); $this->assertSame('10:41:00', $this->formatter->asTime('2014-08-10 12:41:00')); $this->assertSame('1407674460', $this->formatter->asTimestamp('2014-08-10 14:41:00')); $this->assertSame('2014-08-10 12:41:00', $this->formatter->asDatetime('2014-08-10 12:41:00 UTC')); $this->assertSame('2014-08-10', $this->formatter->asDate('2014-08-10 12:41:00 UTC')); $this->assertSame('12:41:00', $this->formatter->asTime('2014-08-10 12:41:00 UTC')); $this->assertSame('1407674460', $this->formatter->asTimestamp('2014-08-10 12:41:00 UTC')); }