PHP DateTime - add() Method
The PHP DateTime::add() method adds the specified interval (days, months, years, hours, minutes and seconds) to the given DateTime object. The date_add() function is an alias of this method.
Syntax
//Object-oriented style public DateTime::add(interval) //Procedural style date_add(object, interval)
Parameters
object |
Required. For procedural style only: A DateTime object returned by date_create(). |
interval |
Required. A DateInterval object specifying the interval to be added.
|
Return Value
Returns the DateTime object with added interval on success, otherwise returns false.
Example: using both styles
The example below shows the usage of DateTime::add() method.
<?php //datetime string $datetime_string = "14-Dec-2015"; //creating a DateTime object $date = new DateTime($datetime_string); //creating a DateInterval object $interval = new DateInterval('P5D'); //adding interval using Object-oriented style $date->add($interval); //formatting the datetime to print it echo $date->format("d-M-Y")."\n"; //adding interval using Procedural style date_add($date, $interval); //formatting the datetime to print it echo $date->format("d-M-Y")."\n"; ?>
The output of the above code will be:
19-Dec-2015 24-Dec-2015
Example: Adding Days, Months & Years
Consider the example below which explains how to add days, months and years to a given DateTime object.
<?php //datetime string $datetime_string = "14-May-2015"; //creating a DateTime object $date = new DateTime($datetime_string); //creating a DateInterval object $interval = new DateInterval('P1Y2M10D'); //formatting the datetime to print it echo "Original date: ".$date->format("d-M-Y")."\n"; //adding 1 year 2 months 10 days $date->add($interval); //formatting the datetime to print it echo "Modified date: ".$date->format("d-M-Y")."\n"; ?>
The output of the above code will be:
Original date: 14-May-2015 Modified date: 24-Jul-2016
Example: Adding Hours, Minutes and Seconds
The example below illustrates how to add hours, minutes and seconds to a given DateTime object.
<?php //datetime string $datetime_string = "14-May-2015"; //creating a DateTime object $date = new DateTime($datetime_string); //creating a DateInterval object $interval = new DateInterval('PT5H30M45S'); //formatting the datetime to print it echo "Original DateTime: ".$date->format("d-M-Y H:i:s")."\n"; //adding 5 hours 30 minutes 45 seconds $date->add($interval); //formatting the datetime to print it echo "Modified DateTime: ".$date->format("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: Adding Days, Months, Years, Hours, Minutes and Seconds
The example below illustrates how to add days, months, years, hours, minutes and seconds to a given DateTime object.
<?php //datetime string $datetime_string = "14-May-2015"; //creating a DateTime object $date = new DateTime($datetime_string); //creating a DateInterval object $interval = new DateInterval('P1Y2M10DT5H30M45S'); //formatting the datetime to print it echo "Original DateTime: ".$date->format("d-M-Y H:i:s")."\n"; //adding 1 year 2 months 10 days 5 hours 30 minutes 45 seconds $date->add($interval); //formatting the datetime to print it echo "Modified DateTime: ".$date->format("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: 24-Jul-2016 05:30:45
Example: Creating DateInterval object from Date String
A DateInterval object can be created using date_interval_create_from_date_string() method. Consider the example below:
<?php //datetime string $datetime_string = "14-May-2015"; //creating a DateTime object $date = new DateTime($datetime_string); //creating a DateInterval object $interval = date_interval_create_from_date_string('1 year 100 days'); //formatting the datetime to print it echo "Original DateTime: ".$date->format("d-M-Y")."\n"; //adding 1 year 100 days $date->add($interval); //formatting the datetime to print it echo "Modified DateTime: ".$date->format("d-M-Y")."\n"; ?>
The output of the above code will be:
Original DateTime: 14-May-2015 Modified DateTime: 22-Aug-2016
Example: Adding Months
Be cautious, when adding months. Consider the example below:
<?php //datetime string $datetime_string = "31-Jan-2015"; //creating a DateTime object $date = new DateTime($datetime_string); //creating a DateInterval object $interval = new DateInterval('P1M'); //formatting the datetime to print it echo "Original DateTime: ".$date->format("d-M-Y")."\n"; //adding 1 month $date->add($interval); //formatting the datetime to print it echo "Modified DateTime: ".$date->format("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