DateTimeImmutable::setTimestamp

(PHP 5 >= 5.5.0)

DateTimeImmutable::setTimestampSets the date and time based on an Unix timestamp

Description

public DateTimeImmutable DateTimeImmutable::setTimestamp ( int $unixtimestamp )

Like DateTime::setTimestamp() but works with DateTimeImmutable.

Коментарии

Автор:
This function will not change the value of the DateTimeImmutable object as the method name might suggest. The object, after all, immutable.

<?php
   $dti 
= new DateTimeImmutable();
   echo 
$dti->getTimestamp(); // e.g. 123456789
   
$dti->setTimestamp(987654321);
   echo 
$dti->getTimestamp(); // 123456789

   
$x $dti->setTimestamp (987654321);
   echo 
$x->getTimestamp(); // 987654321
?>
2021-07-15 14:03:54
http://php5.kiev.ua/manual/ru/datetimeimmutable.settimestamp.html
While modifying Datetime with the timezone, the user should be aware that changing the timestamp using "@".\time() is not the same as changing the timestamp using setTimestamp().

$now = new \DateTimeImmutable('August 30, 2023 09:00:00 GMT+01');
$origin = $now->getTimestamp(); // 1693382400
$usingAt = $now->modify('@'.$now->getTimestamp())->getTimestamp(); // 1693378800
$usingSetTimestamp = $now->setTimestamp($now->getTimestamp())->getTimestamp(); // 1693382400

var_dump($usingAt === $origin); // false
var_dump($usingSetTimestamp === $origin); // true
2023-03-01 10:51:45
http://php5.kiev.ua/manual/ru/datetimeimmutable.settimestamp.html

    Поддержать сайт на родительском проекте КГБ