yii\i18n\Formatter::asTimestamp PHP Method

asTimestamp() public method

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
return string the formatted result.
    public function asTimestamp($value)
    {
        if ($value === null) {
            return $this->nullDisplay;
        }
        $timestamp = $this->normalizeDatetimeValue($value);
        return number_format($timestamp->format('U'), 0, '.', '');
    }

Usage Example

Beispiel #1
0
 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));
 }
All Usage Examples Of yii\i18n\Formatter::asTimestamp