PHP date_modify() Function
The PHP date_modify() function modifies the given DateTime object by specified modifier. This function is an alias of DateTime::modify() method.
Syntax
//Object-oriented style public DateTime::modify(modifier) //Procedural style date_modify(object, modifier)
Parameters
object |
Required. For procedural style only: A DateTime object returned by date_create(). |
modifier |
Required. Specify a date/time string. It should be in valid Date and Time Formats.
|
Return Value
Returns the DateTime object with modified value on success, otherwise returns false.
Example: using both styles
The example below shows the usage of date_modify() function.
<?php //datetime string $datetime_string = "14-Dec-2015"; //creating a DateTime object $date = date_create($datetime_string); //modify using Object-oriented style $date->modify('+10 Days'); //formatting the datetime to print it echo date_format($date, "d-M-Y")."\n"; //modify using Procedural style date_modify($date, '+5 Days'); //formatting the datetime to print it echo date_format($date, "d-M-Y")."\n"; ?>
The output of the above code will be:
24-Dec-2015 29-Dec-2015
Example: Modify by Days, Months & Years
Consider the example below which explains how to modify a given DateTime object by specified days, months and years.
<?php //datetime string $datetime_string = "14-May-2015"; //creating a DateTime object $date = date_create($datetime_string); //formatting the datetime to print it echo "Original date: ".date_format($date, "d-M-Y")."\n"; //modify by 1 year 2 months 10 days date_modify($date, '1 year 2 months 10 days'); //formatting the datetime to print it echo "Modified date: ".date_format($date, "d-M-Y")."\n"; ?>
The output of the above code will be:
Original date: 14-May-2015 Modified date: 24-Jul-2016
Example: Modify by Hours, Minutes and Seconds
The example below illustrates how to modify a given DateTime object by specified hours, minutes and seconds.
<?php //datetime string $datetime_string = "14-May-2015"; //creating a DateTime object $date = date_create($datetime_string); //formatting the datetime to print it echo "Original DateTime: ".date_format($date, "d-M-Y H:i:s")."\n"; //modify by 5 hours 30 minutes 45 seconds date_modify($date, '5 hours 30 minutes 45 seconds'); //formatting the datetime to print it echo "Modified DateTime: ".date_format($date, "d-M-Y H:i:s")."\n"; ?>
The output of the above code will be:
Original DateTime: 14-May-2015 00:00:00 Modified DateTime: 14-May-2015 05:30:45
Example: Modify by Months
Be cautious, when adding or subtracting months. Consider the example below:
<?php //datetime string $datetime_string = "31-Jan-2015"; //creating a DateTime object $date = date_create($datetime_string); //formatting the datetime to print it echo "Original DateTime: ".date_format($date, "d-M-Y")."\n"; //modify by 1 month date_modify($date, '+1 month'); //formatting the datetime to print it echo "Modified DateTime: ".date_format($date, "d-M-Y")."\n"; ?>
The output of the above code will be:
Original DateTime: 31-Jan-2015 Modified DateTime: 03-Mar-2015
❮ PHP Date and Time Reference