Home > PHP > How to Add and Subtract Days, Month and Year From Date in PHP?

How to Add and Subtract Days, Month and Year From Date in PHP?

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.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment