Formats a date, time or datetime in a float number as UNIX timestamp (seconds since 01-01-1970).
public asTimestamp ( integer | string | DateTim\DateTime $value ) : 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 |
Результат | string | the formatted result. |
public function testAsTimestamp() { $value = time(); $this->assertSame("{$value}", $this->formatter->asTimestamp($value)); $this->assertSame("{$value}", $this->formatter->asTimestamp((string) $value)); $this->assertSame("{$value}", $this->formatter->asTimestamp(date('Y-m-d H:i:s', $value))); // null display $this->assertSame($this->formatter->nullDisplay, $this->formatter->asTimestamp(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')); }