DateTimeImmutable::setTimestamp()函数是PHP中的内置函数,用于根据Unix时间戳设置日期和时间。
用法:
DateTimeImmutable DateTimeImmutable::setTimestamp( int $unixtimestamp )
参数:此函数接受单个参数$unixtimestamp,该参数用于设置表示日期的Unix时间戳。
Return Values:此函数返回DateTimeImmutable对象。
以下示例程序旨在说明PHP中的DateTimeImmutable::setTimestamp()函数:
程序1:
// 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');
?>
输出:
1570187170 = 04-10-2019 11:06:10
程序2:
// 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');
?>
输出:
1570187171 = 04-10-2019 11:06:11