DateTime::modify()函数是PHP中的内置函数,可用于修改或更改DateTime对象的时间戳。
用法:
面向对象的样式:DateTime DateTime::modify( string $modify )
程序风格:DateTime date_modify( DateTime $object, string $modify )
参数:此函数使用上面提到和下面描述的两个参数:
$object:它指定由date_create()函数返回的DateTime对象。通过DateTime::modify()函数修改此对象。
$modify:它指定日期/时间字符串。递增或递减以修改DateTime对象。
返回值:如果成功,此函数将返回修改后的DateTime对象,如果失败,则返回False。
以下示例程序旨在说明PHP中的DateTime::modify()函数:
程序1:
// PHP program to illustrate
// DateTime::modify() function
// Creating a DateTime object
$datetime = new DateTime('2019-09-30');
// Calling of date DateTime::modify() function
// with the increment of 5 days as parameters
$datetime->modify('+5 day');
// Getting the modified date in "y-m-d" format
echo $datetime->format('Y-m-d');
?>
输出:
2019-10-05
示例2:
// PHP program to illustrate the
// DateTime::modify() function
// Creating a DateTime object
$datetime = new DateTime('2019-09-30');
// Calling of date DateTime::modify() function
// with the increment of 5 months as parameters
$datetime->modify('+5 month');
// Getting the modified date in "y-m-d" format
echo $datetime->format('Y-m-d');
?>
输出:
2020-03-01