PHP | DateTimeImmutable setTimestamp() Function
Last Updated :
10 Oct, 2019
Improve
The DateTimeImmutable::setTimestamp() function is an inbuilt function in PHP which is used to set the date and time based on an Unix timestamp.
Syntax:
php
php
DateTimeImmutable DateTimeImmutable::setTimestamp( int $unixtimestamp )Parameters: This function accepts single parameter $unixtimestamp which is used to set the Unix timestamp representing the date. Return Values: This function returns the DateTimeImmutable object. Below programs illustrate the DateTimeImmutable::setTimestamp() function in PHP: Program 1:
<?php
// PHP program to illustrate DateTimeImmutable::setTimestamp()
// function
// Creating a new DateTimeImmutable() object
$datetimeImmutable = new DateTimeImmutable();
// Initialising a unixtimestamp
$unixtimestamp = '1171564674';
// Calling the DateTimeImmutable::setTimestamp() function
$a = $datetimeImmutable->setTimestamp($unixtimestamp);
// Getting a new set of date and time in the
// format of 'U = d-m-Y H:i:s'
echo $datetimeImmutable->format('U = d-m-Y H:i:s');
?>
<?php
// PHP program to illustrate DateTimeImmutable::setTimestamp()
// function
// Creating a new DateTimeImmutable() object
$datetimeImmutable = new DateTimeImmutable();
// Initialising a unixtimestamp
$unixtimestamp = '1171564674';
// Calling the DateTimeImmutable::setTimestamp() function
$a = $datetimeImmutable->setTimestamp($unixtimestamp);
// Getting a new set of date and time in the
// format of 'U = d-m-Y H:i:s'
echo $datetimeImmutable->format('U = d-m-Y H:i:s');
?>
Output:
Program 2:
1570187170 = 04-10-2019 11:06:10
<?php
// PHP program to illustrate DateTimeImmutable::setTimestamp()
// function
// Creating a new DateTimeImmutable() object
$datetimeImmutable = new DateTimeImmutable();
// Calling the DateTimeImmutable::setTimestamp() function
// with the parameter of unixtimestamp
$a = $datetimeImmutable->setTimestamp(1291564453);
// Getting a new set of date and time in the
// format of 'U = d-m-Y H:i:s'
echo $datetimeImmutable->format('U = d-m-Y H:i:s');
?>
<?php
// PHP program to illustrate DateTimeImmutable::setTimestamp()
// function
// Creating a new DateTimeImmutable() object
$datetimeImmutable = new DateTimeImmutable();
// Calling the DateTimeImmutable::setTimestamp() function
// with the parameter of unixtimestamp
$a = $datetimeImmutable->setTimestamp(1291564453);
// Getting a new set of date and time in the
// format of 'U = d-m-Y H:i:s'
echo $datetimeImmutable->format('U = d-m-Y H:i:s');
?>
Output:
Reference: https://www.php.net/manual/en/datetimeimmutable.settimestamp.php
1570187171 = 04-10-2019 11:06:11