php date modify,PHP DateTimeImmutable modify()用法及代码示例

DateTimeImmutable::modify()函数是PHP中的内置函数,用于修改或更改创建的DateTimeImmutable对象的时间戳。

用法:

DateTimeImmutable DateTimeImmutable::modify( string $modify )

参数:此函数使用上面提到和下面描述的两个参数:

object:此参数保存date_create()函数返回的DateTime对象。

$modify此参数保存日期/时间字符串,该字符串是更改给定DataTimeImmutable对象的时间集。

返回值:如果成功,此函数将返回修改后的DateTimeImmutable对象;如果失败,则返回False。

以下示例程序旨在说明PHP中的DateTimeImmutable::modify()函数:

示例1:该程序以5天为增量修改给定日期。

// PHP program to illustrate DateTimeImmutable::modify()

// function

// creating a DateTime object

$datetimeImmutable = new DateTimeImmutable('2019-10-02T00:00:00');

// Calling of date DateTimeImmutable::modify() function

// with the increment of 5 days as parameters

$newDateTimeImmutable = $datetimeImmutable->modify('+5 days');

// Getting the modified date in "y-m-d" format

echo $newDateTimeImmutable->format('Y-m-d');

?>

输出:

2019-10-07

示例2:该程序以2个月为增量修改给定日期。

// PHP program to illustrate DateTimeImmutable::modify()

// function

// Creating a DateTime object

$datetimeImmutable = new DateTimeImmutable('2019-10-02T00:00:00');

// Calling of date DateTimeImmutable::modify() function

// with the increment of 2 months as parameters

$newDateTimeImmutable = $datetimeImmutable->modify('+2 months');

// Getting the modified date in "y-m-d" format

echo $newDateTimeImmutable->format('Y-m-d');

?>

输出:

2019-12-02

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值