Here’s how you add and subtract dates from one another. For example, this simple code can calculate what the date will be 2 weeks before or after 1998-08-14 (yyyy-mm-dd).
Subtracting days from a date
The following example will subtract 3 days from 1998-08-14. The result will be 1998-08-11.
$date = “1998-08-14”;
$newdate = strtotime ( ‘-3 day’ , strtotime ( $date ) ) ;
$newdate = date ( ‘Y-m-j’ , $newdate );
echo $newdate;
Subtracting Weeks from a date
The following example will subtract 3 weeks from 1998-08-14. The result will be 1998-07-24. Notice that the only difference in the code is the week statement.
$date = “1998-08-14”;
$newdate = strtotime ( ‘-3 week’ , strtotime ( $date ) ) ;
$newdate = date ( ‘Y-m-j’ , $newdate );
echo $newdate;
Subtracting Months from a date
The following example will subtract 3 months from 1998-08-14. The result will be 1998-05-14. Notice that the only difference in the code is the month statement.
$date = “1998-08-14”;
$newdate = strtotime ( ‘-3 month’ , strtotime ( $date ) ) ;
$newdate = date ( ‘Y-m-j’ , $newdate );
echo $newdate;
Subtracting Years from a date
The following example will subtract 3 years from 1998-08-14. The result will be 1995-08-14. Notice that the only difference in the code is the year statement.
$date = “1998-08-14”;
$newdate = strtotime ( ‘-3 year’ , strtotime ( $date ) ) ;
$newdate = date ( ‘Y-m-j’ , $newdate );
echo $newdate;
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’
Leave a comment