DateTimeImmutable::setTime()函数是PHP中的内置函数,用于通过重置创建的DateTimeImmutable对象的当前时间来设置所需时间。
用法:
DateTimeImmutable DateTimeImmutable::setTime( int $hour, int $minute,
int $second, int $microseconds )
参数:该函数接受上述和以下所述的四个参数:
$hour:此参数用于设置小时时间。
$minute:此参数用于设置分钟时间。
$second:此参数用于设置第二次。
$microseconds:此参数用于设置微秒时间。
返回值:此函数返回一个新的日期时间对象。
以下示例程序旨在说明PHP中的DateTimeImmutable::setTime()函数:
示例1:
// PHP program to illustrate DateTimeImmutable::setTime()
// function
// Creating a new DateTimeImmutable() object
$datetimeImmutable = new DateTimeImmutable('2019-10-04');
// Initialising Hour, Minute and Second
$Hour = '04';
$Minute = '10';
$Second = '40';
// Calling the DateTimeImmutable::setTime() function
$a = $datetimeImmutable->setTime($Hour, $Minute, $Second);
// Getting a new set of date and time in the
// format of 'Y-m-d H:i:s'
echo $a->format('Y-m-d H:i:s');
?>
输出:
2019-10-04 04:10:40
示例2:
// PHP program to illustrate DateTimeImmutable::setTime()
// function
// Creating a new DateTimeImmutable() object
$datetimeImmutable = new DateTimeImmutable('2019-10-04');
// Calling the setTime() function
// with parameters like Hour of 10,
// minute of 33 and second of 39
$a = $datetimeImmutable->setTime(10, 33, 39);
// Getting a new set of date and time in the
// format of 'Y-m-d H:i:s'
echo $a->format('Y-m-d H:i:s');
?>
输出:
2019-10-04 10:33:39