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);
Call
What 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
Parameter
Description
object
Required. The DateTime object to change.
timestamp
Required. The Unix timestamp in seconds.
Return Value
Return Value
Description
DateTime
The 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.
Thirty days is 30 times 86400 seconds, which is added to the timestamp.
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.