php datetime time(),PHP DateTimeImmutable setTime()用法及代码示例

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值