Add And Subtract PHP Dates

  • Update On January 6th, 2017
  • in PHP

This post was last updated on January 6th, 2017 at 07:18 pm

Date function is a very important function in any technology and in PHP it is quite easily understandable and In this post we are going to learn how to add days, weeks, months, year in PHP.

Adding dates is easy in PHP and once you come to know this you don’t need to memorize at all.

Subtracting days from a date

The following example will subtract 3 days from 2016-01-14. The result will be 2016-01-11.

$date = "2016-01-14";
$newdate = strtotime ( '-3 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );

echo $newdate; //2016-01-11

Subtracting Weeks from a date

The following example will subtract 3 weeks from 2014-08-14. The result will be 2014-07-24. Notice that the only difference in the code is the week statement.

$date = "2014-08-14";
$newdate = strtotime ( '-3 week' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate; //2014-07-24

Subtracting Months from a date

The following example will subtract 3 months from 2014-08-14. The result will be 2014-05-14. Notice that the only difference in the code is the month statement.

$date = "2014-08-14";
$newdate = strtotime ( '-3 month' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate; //2014-05-14

Subtracting Years from a date

The following example will subtract 3 years from 2014-08-14. The result will be 2011-08-14. Notice that the only difference in the code is the year statement.

$date = "2014-08-14";
$newdate = strtotime ( '-3 year' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate; //2011-08-14

Adding days, months, weeks and years from a date

There isn’t really much difference from subtracting and adding dates. To add dates, just use any of the examples above and replace the negative (-) with a positive (+) e.g. ‘+3 weeks’

$date = date("Y-m-d");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +12 month");
$date = date("Y-m-d",$date);
echo $date; //2017-02-24

Other examples

$date = date("2016-02-24");
$date1 = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
echo date("Y-m-d",$date1); //2016-02-25
$date2 = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
echo date("Y-m-d",$date2); //2016-03-02
$date3 = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
echo date("Y-m-d",$date3); //2016-03-09
$date4 = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
echo date("Y-m-d",$date4); //2016-03-24
$date5 = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
echo date("Y-m-d",$date5); //2016-03-25
About This Author

My name is Parameshwar Roy (P. Roy), and I am a web developer. This is my personal blog to record my own thoughts. Though I am not a natural writer, I love to share my experiences. Hope my experiences will be useful to you...read more about me

Leave A Reply