
Today in this tutorial, we will see how to add or subtract days, month, and year from date using PHP. This is a very simple and very short point-to-point tutorial.
You will see how you can add or subtract 1 day, 5 days, 1 month, or even 1 year from any date. You can also add or subtract days from the current date using these tutorial references.
So let’s see some examples.
Add Days, Month, and Year to Date in PHP
In this case, we will see how to add days, months, and years to the current date or any date. We will use built-in functions like date() and strtotime(). See the following examples.
Add +3 days to date
Output:
2022-01-23
Add +7 days to date
Output:
2022-01-27
Add +1 month to date
Output:
2022-02-22
Add +1 year to date
Output:
2023-01-22
Add days to current date
As see the above example to add the days to date in given date, same we can do with the current date. We just have to go with only the date() function and remove the given date variable. See the following reference.
Output:
2023-02-10
Subtract Days, Month, and Year from Date in PHP
Now, we will see how to subtract days, months, and years to the current date or any date. We will use the same built-in functions like the date() and strtotime(). See the following examples. Only we just have to subtract instead of add.
Subtract -3 days from date
Output:
2022-01-17
Subtract -7 days from date
Output:
2022-01-13
Subtract -1 month from date
2021-12-22
Output:
Subtract -1 year from date
Output:
2021-01-22
Subtract days from current date
Output:
2022-01-10
As you see all the above examples, it is very easy to add or subtract days, month, and year from date using PHP core functions.
See Also: PHP Builtin Date and Time Functions
See Also: How to Calculate Age from Date of Birth in PHP?