English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Manuel des fonctions Date & Time PHP
La fonction date_timestamp_set() définit la date et l'heure basées sur le timestamp Unix.
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)
Serial 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 initially introduced in PHP version5.3introduced in version 5.2.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:
$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.-
$date = new DateTime(); $timestamp1 = time() - (23*12*30); $res1 = date_timestamp_set($date, $timestamp1); print("Date: ".date_format($res1, "Y/m/d H:i:s")); print("\n"); $timestamp2 = time() + (23*12*30); $res2 = date_timestamp_set($date, $timestamp2); 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 "@"
$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 to15Will be added three months at the correct time
Date: 2020/05/11 00:15:36