date_add()
The date_add()
function takes an existing DateTime
object and adds a specified number of days, months, years, hours, minutes or seconds.
Syntax
date_add(date_object, time_frame_to_add)
The date_object
parameter is an existing DateTime
object.
The time_frame_to_add
parameter is a DateInterval
object.
Example
The following example uses date_create()
to create a new DateTime
object.
The DateTime
object is then used as the first parameter of date_add()
. The second parameter is a DateInterval
object, created by the built-in function date_interval_create_from_date_string()
. This function accepts a string containing a numeric value followed by a unit of time.
Note: The unit of time can be written in singular or plural form.
The output is then printed to the console, using date_format()
to format the string.
<?php$my_date = date_create("2023-07-10 14:16:33");date_add($my_date, date_interval_create_from_date_string("3 days"));echo date_format($my_date,"Y-m-d G:i:s");?>
The code results in the following:
2023-07-13 14:16:33
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.