PHP date_timestamp_get() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Date/Time

Definition

In PHP, date_timestamp_get() returns the Unix timestamp of a date object, which is the number of seconds since 1 January 1970 00:00:00 UTC.

It is the procedural name for DateTime::getTimestamp() and DateTimeImmutable::getTimestamp(). A timestamp is a single number that describes an exact moment.

Syntax

Call date_timestamp_get() with the values you need. Start with this line.

PHP
$timestamp = date_timestamp_get($date);
CallWhat it does
date_timestamp_get($date2024)Returns 1704067200 for 2024-01-01 00:00:00 UTC.
date_timestamp_get($epoch)Returns 0 for 1970-01-01 00:00:00 UTC.
date_timestamp_get($before)Returns a negative number for dates before 1970.

A timestamp does not store a timezone. Two dates that show different clock times in different timezones can still have the same timestamp. In new code you can call $date->getTimestamp() on a DateTimeImmutable.

Library declarations
PHP
date_timestamp_get(DateTimeInterface $object): int

Parameters

ParameterDescription
objectRequired. A DateTime or DateTimeImmutable object.

Return Value

Return ValueDescription
intThe Unix timestamp. It is negative for dates before 1970.

PHP Examples

Example 1 — Get the Timestamp of a Date

Create dates in UTC and read their timestamps.

PHP
<?php
$utc = timezone_open("UTC");

$a = date_create("2024-01-01 00:00:00", $utc);
$b = date_create("2000-01-01 00:00:00", $utc);
$c = date_create("1970-01-01 00:00:00", $utc);

echo date_timestamp_get($a), PHP_EOL;
echo date_timestamp_get($b), PHP_EOL;
echo date_timestamp_get($c), PHP_EOL;

How It Works

  1. Create a UTC timezone so the numbers are the same everywhere.
  2. Each date is built with date_create() using that timezone.
  3. date_timestamp_get() counts the seconds since January 1, 1970.
  4. The date 1970-01-01 00:00:00 UTC is the starting point, so its timestamp is 0.

Example 2 — Timezones and Dates Before 1970

The same moment gives the same timestamp in any timezone. Dates before 1970 are negative.

PHP
<?php
$utc = date_create("2024-01-01 00:00:00", timezone_open("UTC"));
$india = date_create("2024-01-01 05:30:00", timezone_open("Asia/Kolkata"));
$before = date_create("1969-12-31 23:59:59", timezone_open("UTC"));

echo date_timestamp_get($utc), PHP_EOL;
echo date_timestamp_get($india), PHP_EOL;
echo date_timestamp_get($before), PHP_EOL;

How It Works

  1. The first date is midnight on January 1, 2024 in UTC.
  2. In India it is 5 hours 30 minutes later on the clock, so 05:30 in Asia/Kolkata is the same moment.
  3. Both dates give 1704067200, because a timestamp does not depend on the timezone.
  4. One second before the start of 1970 is -1.

Example 3 — Count Days with Timestamps

Subtract two timestamps and divide by the number of seconds in a day.

PHP
<?php
$utc = timezone_open("UTC");

$start = date_create("2024-01-01", $utc);
$end = date_create("2024-03-01", $utc);

$seconds = date_timestamp_get($end) - date_timestamp_get($start);
echo "Days between: ", intdiv($seconds, 86400), PHP_EOL;

How It Works

  1. Create dates for January 1 and March 1, 2024 in UTC.
  2. Subtract the start timestamp from the end timestamp to get the difference in seconds.
  3. A day has 86400 seconds, so intdiv() turns seconds into whole days.
  4. January has 31 days and February 2024 has 29, so the answer is 60.

Frequently Asked Questions

It returns the Unix timestamp of a date object, which is the number of seconds since 1 January 1970 at 00:00:00 UTC.
No. A timestamp always describes one exact moment. Only the way it is shown on a clock changes between timezones.
Dates before January 1, 1970 are counted backwards from the epoch, so their timestamps are negative.
Use time(), or call date_timestamp_get(date_create()). Both return the current Unix timestamp.
Yes. It is the procedural alias of getTimestamp() and works with both DateTime and DateTimeImmutable objects.

Next: date_timestamp_set()

date_timestamp_set() changes a DateTime object to the date and time described by a Unix ti.

date_timestamp_set() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful