English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP Date & Time Function Manual
The date_timestamp_set() function sets the date and time based on Unix timestamp.
The date_timestamp_set() function is an alias for DateTime::setTimestamp. This function accepts a DateTime object and a Unix timestamp as parameters and sets the specified timestamp to the given object.
date_timestamp_set($object, $timestamp)
Number | Parameters and Description |
---|---|
1 | object(Required) This is a DateTime object. |
2 | timestamp(Required) This is the Unix timestamp. |
The PHP date_timestamp_set() function returns a DateTime object with the modified (time) value. If it fails, this function will return a boolean valuefalse.
This function was originally introduced in PHP version5.3introduced in version 5.3.0 and can be used in all higher versions.
The following example demonstratesdate_timestamp_setThe function specifies the date and time of the Unix timestamp:
<?php $date = new DateTime(); $res = date_timestamp_set($date, 1505292545); print("Date: ".date_format($res, "Y");/m/d H:i:s")); ?>Test to see‹/›
Output Result
Date: 2017/09/13 08:49:05
The following example creates a DateTime object and usesdate_timestamp_set()The function modifies its value.-
<?php $date = new DateTime(); $timestamp1 = time() - (23*12*30); $res1 = date_timestamp_set($date,1); print("Date: ".date_format($res1, "Y/m/d H:i:s")); print("\n"); $timestamp2 = time() + (23*12*30); $res2 = date_timestamp_set($date,2); print("Date: ".date_format($res2, "Y/m/d H:i:s")); ?>Test to see‹/›
Output Result
Date: 2020/05/11 08:57:30 Date: 2020/05/11 13:33:30
As an alternative to this function, you can pass the timestamp value as a string and pass it to the DateTime constructor as a parameter "@"
<?php $date = new DateTime("@1495283256 print("Date: ".date_format($date, "Y");/m/d H:i:s"));?Test to see‹/›
Because we have set the month value to15It will be added three months at the correct time
Date: 2020/05/11 00:15:36