PHP 7.2.0 Release Candidate 4 Released


(PHP 5 >= 5.3.0)

DateTime::getTimestampدریافت برچسب زمانی Unix


Oriented object style

public int DateTime::getTimestamp ( void )

Procedural style

int date_timestamp_get ( DateTime $object )

دریافت برچسب زمانی Unix.


This function has no parameters.

Return Values

دریافت برچسب زمانی Unix نمایش‌دهنده تاریخ.


Example #1 مثال DateTime::getTimestamp()

Oriented object style

= new DateTime();

Procedural style


The above examples will output something similar to:



استفاده از U به عنوان پارامتر برای DateTime::format() به هنگام استفاده از PHP 5.2 جایگزین مورد استفاده است.

See Also

  • DateTime::setTimestamp() - تنظیم تاریخ و زمان براساس برچسب زمانی Unix
  • DateTime::format() - بازگرداندن تاریخ قالب‌بندی شده متناسب با قالب داده شده
add a note add a note

User Contributed Notes 4 notes

Justin Heesemann
7 years ago
Note that for dates before the unix epoch getTimestamp() will return false, whereas format("U") will return a negative number.

= new DateTime("1899-12-31");
// "-2209078800"
echo $date->format("U");
// false
echo $date->getTimestamp();
heiccih at gmail dot com
3 years ago
In 32-bit system the unix timestamp will overflow if the date goes beyond year 2038 and this method will return false. In 64-bit systems this function will still work as intended. For more information please see
1 year ago
Miguel's comment is wrong - getTimeStamp() always returns a timestamp, this has nothing to do with timezones at all, as you can see below.

What's really happening in Miguel's example, is he is parsing the input dates in different timezones.

There is no such thing as "a timestamp relative to the timezone".


= new DateTime('2016-03-11 11:00:00', new DateTimeZone('Europe/Rome'));

var_dump($d->getTimestamp()); // 1457690400

$d->setTimeZone(new DateTimeZone('America/New_York'));

var_dump($d->getTimestamp()); // 1457690400
miguelmuscat93 at gmail dot com
1 year ago
Note that getTimestamp() does not return the UTC timestamp. It returns the timestamp relative to the set timezone, or the default server timezone. This also applies to dates in timezones with Daylight Savings.

// 11th March 2016 @ 11:00 UTC has timestamp: 1457694000
$d1 = new DateTime('2016-03-11 11:00:00', new DateTimeZone('Europe/Rome'));
$t1 = $d1->getTimestamp();

// 11th April 2016 @ 11:00 UTC has timestamp: 1460372400
$d2 = new DateTime('2016-04-11 11:00:00', new DateTimeZone('Europe/Rome'));
$t2 = $d2->getTimestamp();

printf("11 March: %d (diff = %d less)\n", $t1, 1457694000 - $t1);
printf("11 April: %d (diff = %d less)", $t2, 1460372400 - $t2);

// Prints:
// 1457690400 (diff = 3600)
// 1460365200 (diff = 7200)
To Top