How to Add or Subtract Date Interval In PHP

How to Add or Subtract Date Interval In PHP
Adding or subtracting interval to a DateTime instance is quite easy. Two methods we are going to described in this post. Both methods belong to DateTime object itself.

If you are using PHP 5.3+ version, than simply add and subtract method work for it.

See below example to Add 2 Day interval in date.

PID use for 1 day time interval, If you need to add 2 days, 1 month and 4 years than use P4Y1M2D.  To include time period interval format like that P1DT2H. It will add 1 day and 2 hours interval in date time.

To Subtract interval in DateTime check below example:

For PHP 5.2 version simple modify method work for date interval. To add three days interval, just use ‘+3 day’. Check example code:

So use these simple method to Add or Subtract date time interval. In our previously blog post described Changing Date and Time Format. Share this post with others. Thanks

Recommended Posts For You

About Harish

I am professional web developer and blogger. Use this blog to share own api and other web development experience with you. I do accept paid work. Write to me at - [email protected]

View all posts by Harish

Leave a Comment

Your email address will not be published. Required fields are marked *