Add days to date in PHP
Question:
How to add days to date in PHP? Answer:
$date = new DateTime('2020-11-24');
$date->add(new DateInterval("P9D"));
echo $date->format('Y-m-d');
Description:
You can add any number of days to a given date using DateTime
's add method and the appropriate DateInterval
. First, create a DateTime
object from the base date. Then create a DateInterval
object with a constructor parameter in a format 'P' + NUMBER_OF_DAYS + 'D'
. For example, if you want to add 9 days then the format string is P9D
. Then all you have to do is add this interval to the original date using the add
method.
Reference:
PHP DateInterval reference
Share "How to add days to date in PHP?"
Related snippets:
- Add minutes to date time in PHP
- Add days to date in PHP
- Get same day in the next week in PHP
- Get time difference in minutes in PHP
- Convert date to timestamp in PHP
- Convert timestamp to DateTime in PHP
- Get the last day of a month from date in PHP
- Get the number of days between two dates in PHP
- Get yesterday's date in PHP
- Get tomorrow's date in PHP
- Get current time in PHP
- Get actual date in PHP
Tags:
add days to date, add number of days to date, adding days to date Technical term:
Add days to date in PHP