DateTimeInterface::getTimestamp
DateTimeImmutable::getTimestamp
DateTime::getTimestamp
date_timestamp_get
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
DateTimeInterface::getTimestamp -- DateTimeImmutable::getTimestamp -- DateTime::getTimestamp -- date_timestamp_get — Gets the Unix timestamp
Description
Object-oriented style
public function DateTimeInterface::getTimestamp():
int
public function DateTimeImmutable::getTimestamp():
int
public function DateTime::getTimestamp():
int
Parameters
This function has no parameters.
Return Values
Returns the Unix timestamp representing the date.
Examples
Example #1 DateTime::getTimestamp() example
<?php
$date = new DateTimeImmutable();
echo $date->getTimestamp();
The above example will output
something similar to:
<?php
$date = date_create();
echo date_timestamp_get($date);
The above example will output
something similar to:
If you need to retrieve the timestamp with millisecond or
microsecond resolution, then you can use the
DateTimeInterface::format() function.
Example #2 Retrieving timestamp with milli and microsecond resolution
<?php
$date = new DateTimeImmutable();
$milli = (int) $date->format('Uv'); // Timestamp in milliseconds
$micro = (int) $date->format('Uu'); // Timestamp in microseconds
echo $milli, "\n", $micro, "\n";
The above example will output
something similar to:
1674057635586
1674057635586918