Skip to main content

A few examples of adding and subtracting dates objects.

<?php

// -----------------------------------------------------------------
// 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;

?>