This post is about How to add and subtract dates from one another.
For example, this simple code can calculate what the date will be 5 weeks before or after from any specific date like: 2016-01-06 (yyyy-mm-dd).
Subtracting days from a date
The following example will subtract 2 days from 2016-01-06.
$Date = "2016-01-06";
$sub_newdate = date('Y-m-d', strtotime($Date. ' - 2 day'));
Adding days into a date
$add_newdate = date('Y-m-d', strtotime($Date. ' + 2 day'));
echo $sub_newdate;
echo '<br/>';
echo $add_newdate;
1 2 3 4 5 6 7 8 9 |
$Date = "2016-01-06"; $sub_newdate = date('Y-m-d', strtotime($Date. ' - 2 day')); Adding days into a date $add_newdate = date('Y-m-d', strtotime($Date. ' + 2 day')); echo $sub_newdate; echo '<br/>'; echo $add_newdate; |
Subtracting Weeks from a date
The following example will subtract 2 weeks from 2016-01-06.
$Date = "2016-01-06";
$sub_newdate = date('Y-m-d', strtotime($Date. ' - 2 week'));
Adding Weeks into a date
$add_newdate = date('Y-m-d', strtotime($Date. ' + 2 week'));
echo $sub_newdate;
echo '<br/>';
echo $add_newdate;
1 2 3 4 5 6 7 8 9 10 |
$Date = "2016-01-06"; $sub_newdate = date('Y-m-d', strtotime($Date. ' - 2 week')); Adding Weeks into a date $add_newdate = date('Y-m-d', strtotime($Date. ' + 2 week')); echo $sub_newdate; echo '<br/>'; echo $add_newdate; |
Subtracting Months from a date
The following example will subtract 2 months from 2016-01-06.
$Date = "2016-01-06";
$sub_newdate = date('Y-m-d', strtotime($Date. ' - 2 month'));
Adding months into a date
$add_newdate = date('Y-m-d', strtotime($Date. ' + 2 month'));
echo $sub_newdate;
echo '<br/>';
echo $add_newdate;
1 2 3 4 5 6 7 8 9 10 |
$Date = "2016-01-06"; $sub_newdate = date('Y-m-d', strtotime($Date. ' - 2 month')); Adding months into a date $add_newdate = date('Y-m-d', strtotime($Date. ' + 2 month')); echo $sub_newdate; echo '<br/>'; echo $add_newdate; |
Subtracting Years from a date
The following example will subtract 2 years from 2016-01-06.
$Date = "2016-01-06";
$sub_newdate = date('Y-m-d', strtotime($Date. ' - 5 year'));
Adding Years into a date
$add_newdate = date('Y-m-d', strtotime($Date. ' + 5 year'));
echo $sub_newdate;
echo '<br/>';
echo $add_newdate;
1 2 3 4 5 6 7 8 9 10 |
$Date = "2016-01-06"; $sub_newdate = date('Y-m-d', strtotime($Date. ' - 5 year')); Adding Years into a date $add_newdate = date('Y-m-d', strtotime($Date. ' + 5 year')); echo $sub_newdate; echo '<br/>'; echo $add_newdate; |
Subtracting Years from a date
The following example will subtract 5 years,5 months,5 days from 2016-01-06.
$Date = "2016-01-06";
$sub_newdate = date('Y-m-d', strtotime($Date. ' - 5 years - 5 months - 5 days'));
Adding Years,months,days into a date
$add_newdate = date('Y-m-d', strtotime($Date. ' + 5 years + 5 months + 5 days'));
echo $sub_newdate;
echo '<br/>';
echo $add_newdate;
1 2 3 4 5 6 7 8 9 10 |
$Date = "2016-01-06"; $sub_newdate = date('Y-m-d', strtotime($Date. ' - 5 years - 5 months - 5 days')); Adding Years,months,days into a date $add_newdate = date('Y-m-d', strtotime($Date. ' + 5 years + 5 months + 5 days')); echo $sub_newdate; echo '<br/>'; echo $add_newdate; |