How to Add or Subtract Days From a Date Using PHP

How to Add or Subtract Days From a Date Using PHP

In this article, we will see one of the easiest way to add or subtract days to a specified date forms. PHP has a very interesting and complete to obtain or format a date function. The function date () in PHP, we will use it to get the current date in the format which we want.

The main objective of this article is not to know how the function date () works in PHP, but rather, add or subtract a number of days to date. We are going to use the function strtotime ().  A very interesting feature that convert a description of date / time in English text to a Unix date.

For example, if we add 30 days or a month to the current date, we could do as follows:

$ current_date  =  date ( 'dm-Y' ) ; 
$ future_date  =  strtotime ( '+30 day' ,  strtotime ( $ current_date ) ) ; 
$ future_date  =  date ( 'dm-Y' ,  $ future_date ) ;

We’re just getting the current date and get future date by adding 30 days.

And if we subtract 30 days or a month to the current date, we could do as follows:

$ current_date  =  date ( 'dm-Y' ) ; 
$ past_date  =  strtotime ( '-30 day' ,  strtotime ( $ current_date ) ) ; 
$ past_date  =  date ( 'dm-Y' ,  $ past_date ) ;
Lucid Kit

Related Posts
Leave a reply
Captcha Click on image to update the captcha .