English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP Date & Time Function Manual
The date_modify() function modifies the value of the date-time (DateTime) object
The date_modify() function is an alias of DateTime::modify(). This function is used to modify the date in the DateTime object. It changes the timestamp of the given object.
date_modify($object, $modify)
Serial number | Parameters and descriptions |
---|---|
1 | object (required) This indicates the DateTime object you want to modify. |
2 | modify (required) This is the date/time string specifying the required modification. |
Returns the modified DateTime object. If it fails, this function will return a boolean valuefalse.
This function was initially introduced in PHP version5.2introduced in version 5.2.0 and can be used in all higher versions.
The following examples demonstratedate_modify()function usage-
<?php //Modify date $date = date_modify(new DateTime(), "+15 day"); print("Date: ".date_format($date, "Y/m/d"); ?>Test and see‹/›
Output result
Date: 2020/05/21
The following example creates a DateTime object and usesdate_modify()function to modify its date.-
<?php //Create a DateTime object $date_time_Obj = date_create("25-09-1989"); print("Original date: ".date_format($date_time_Obj, "Y/m/d"); print("\n"); //Set date $date = date_modify($date_time_Obj, "+15 years 7 months 23 days"); print("Modify date: ".date_format($date, "Y/m/d"); ?>Test and see‹/›
Output result
Original date: 1989/09/25 Modify date: 2005/05/18
You can also modify the date by specifying the number of weeks, such as
<?php //Create DateTime object $date_time_Obj = date_create("25-09-1989"); print("Original date: ".date_format($date_time_Obj, "Y/m/d"); print("\n"); //Set date $date = date_modify($date_time_Obj, "1960 weeks"); print("Modify date: ".date_format($date, "Y/m/d"); ?>Test and see‹/›
This will produce the following output-
Original date: 1989/09/25 Modify date: 2027/04/19
will add the specified date1day
<?php $date = new DateTime("1990-12-12"); $date->modify("+1 day"); echo $date->format("Y-m-d"); ?>Test and see‹/›
This will produce the following output-
1990-12-13