PHP date_timestamp_set() Function

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

Definition

In PHP, date_timestamp_set() changes a DateTime object to the date and time described by a Unix timestamp.

It is the procedural name for DateTime::setTimestamp() (and DateTimeImmutable::setTimestamp()). The object keeps its own timezone, and the clock time is calculated from the timestamp.

Syntax

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

PHP
date_timestamp_set($date, 1704067200);
CallWhat it does
date_timestamp_set($date, 1704067200)Sets the date to 2024-01-01 00:00:00 UTC.
date_timestamp_set($date, 0)Sets the date to the Unix epoch, 1970-01-01 00:00:00 UTC.
date_timestamp_set($date, $ts + 86400)Moves the date forward by one day (86400 seconds).

The object keeps its timezone, so the same timestamp shows different clock times in different timezones. The function changes the object you pass in. For a copy, use DateTimeImmutable::setTimestamp().

Library declarations
PHP
date_timestamp_set(DateTime $object, int $timestamp): DateTime

Parameters

ParameterDescription
objectRequired. The DateTime object to change.
timestampRequired. The Unix timestamp in seconds.

Return Value

Return ValueDescription
DateTimeThe same DateTime object after the timestamp has been applied.

PHP Examples

Example 1 — Set a Date from a Timestamp

Create any date object, then replace its moment with a timestamp.

PHP
<?php
date_default_timezone_set("UTC");

$date = date_create();

date_timestamp_set($date, 1704067200);
echo date_format($date, "Y-m-d H:i:s"), PHP_EOL;

date_timestamp_set($date, 946684800);
echo date_format($date, "Y-m-d H:i:s"), PHP_EOL;

date_timestamp_set($date, 0);
echo date_format($date, "Y-m-d H:i:s"), PHP_EOL;

How It Works

  1. date_create() makes an object for the current moment. Its value is replaced in the next steps.
  2. Timestamp 1704067200 is midnight on January 1, 2024 in UTC.
  3. Timestamp 946684800 is the start of the year 2000.
  4. Timestamp 0 is the Unix epoch.

Example 2 — The Timezone Stays the Same

The object keeps its own timezone, so the clock time changes while the moment is the same.

PHP
<?php
$date = date_create("now", timezone_open("Asia/Kolkata"));

date_timestamp_set($date, 1704067200);
echo date_format($date, "Y-m-d H:i:s P"), PHP_EOL;

date_timestamp_set($date, 0);
echo date_format($date, "Y-m-d H:i:s P"), PHP_EOL;

How It Works

  1. The date object uses the Asia/Kolkata timezone.
  2. Timestamp 1704067200 is midnight UTC, which is 05:30 in India.
  3. The format character P prints the offset, which stays +05:30.
  4. The Unix epoch (timestamp 0) also shows as 05:30 in India.

Example 3 — Move a Date by 30 Days

Read the timestamp, add seconds, then set it back.

PHP
<?php
date_default_timezone_set("UTC");

$date = date_create("2024-01-01 00:00:00");
$timestamp = date_timestamp_get($date);

date_timestamp_set($date, $timestamp + 30 * 86400);
echo date_format($date, "Y-m-d H:i:s"), PHP_EOL;

How It Works

  1. Create a date for January 1, 2024.
  2. date_timestamp_get() reads its timestamp.
  3. Thirty days is 30 times 86400 seconds, which is added to the timestamp.
  4. date_timestamp_set() applies the new number, giving January 31.

Frequently Asked Questions

It changes a DateTime object to the moment described by a Unix timestamp. The object keeps its timezone, so the clock time is worked out from that zone.
No. The timezone stays the same. Only the date and time change to match the timestamp.
Yes, for DateTime it changes the object in place. DateTimeImmutable::setTimestamp() returns a new object instead.
Use date_timestamp_get($date) or $date->getTimestamp(). They return the number of seconds since January 1, 1970 UTC.
Yes. It is the procedural alias of setTimestamp(), which also exists on DateTimeImmutable.

Next: date_timezone_get()

date_timezone_get() returns the timezone of a date object as a DateTimeZone object.

date_timezone_get() 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