Formats the value as a datetime.
public asDatetime ( 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, [[dateFormat]] 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. |
return | string | the formatted result. |
public function testAsDatetime() { $value = time(); $this->assertSame(date('M j, Y g:i:s A', $value), $this->formatter->asDatetime($value)); $this->assertSame(date('Y/m/d h:i:s A', $value), $this->formatter->asDatetime($value, 'php:Y/m/d h:i:s A')); // null display $this->assertSame($this->formatter->nullDisplay, $this->formatter->asDatetime(null)); }
public function asDatetime($value, $format = null) { if ($this->calendar->code == 'gregorian') { return parent::asDatetime($value, $format); } if ($format === null) { $format = $this->datetimeFormat; } return $this->formatDateTimeValue($value, $format, 'datetime'); }
/** * Formats the value as a datetime. * @param integer|string|DateTime $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](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 * * @param string $format the format used to convert the value into a date string. * If null, [[dateFormat]] 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/de/function.date.php)-function. * * @return string the formatted result. * @throws InvalidParamException if the input value can not be evaluated as a date value. * @throws InvalidConfigException if the date format is invalid. * @see datetimeFormat */ public function asDatetime($value, $format = null) { switch ($this->locale) { case 'ru': return $this->datetimeAsHumanRu($value, $format); case 'en': return $this->datetimeAsHumanEn($value, $format); default: return parent::asDatetime($value, $format); } }
public function testAsDatetime() { $value = time(); $this->assertSame(date('M j, Y g:i:s A', $value), $this->formatter->asDatetime($value)); $this->assertSame(date('Y/m/d h:i:s A', $value), $this->formatter->asDatetime($value, 'php:Y/m/d h:i:s A')); // empty input $this->assertSame('Jan 1, 1970 12:00:00 AM', $this->formatter->asDatetime('')); $this->assertSame('Jan 1, 1970 12:00:00 AM', $this->formatter->asDatetime(0)); $this->assertSame('Jan 1, 1970 12:00:00 AM', $this->formatter->asDatetime(false)); // null display $this->assertSame($this->formatter->nullDisplay, $this->formatter->asDatetime(null)); }
/** * Formats the value as a datetime. * @param integer|string|DateTime $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](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 * * @param string $format the format used to convert the value into a date string. * If null, [[dateFormat]] 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/de/function.date.php)-function. * * @return string the formatted result. * @throws InvalidParamException if the input value can not be evaluated as a date value. * @throws InvalidConfigException if the date format is invalid. * @see datetimeFormat */ public function asDatetime($value, $format = null) { if ($format == 'human') { switch ($this->locale) { case 'ru': return $this->datetimeAsHumanRu($value); break; case 'en': return $this->datetimeAsHumanEn($value); break; } } return parent::asDatetime($value, $format); }
/** * 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')); }
/** * @inheritdoc */ public function asDatetime($value, $format = null) { return str_replace(['January', 'February', 'Match', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], ['янв', 'фев', 'мар', 'апр', 'мая', 'июн', 'июль', 'авг', 'сен', 'окт', 'ноя', 'дек'], parent::asDatetime($value, $format)); }
public function asDatetimeFiltered($value, $format = null) { return $value ? parent::asDatetime($value, $format) : null; }
/** * @inheritdoc * Adds support for [-]infinity. */ public function asDatetime($value, $format = null) { if (($label = $this->isInfinity($value)) !== null) { return $label; } return parent::asDatetime($value, $format); }