How do I remove 3 months from a date?

$new_timestamp = strtotime('-3 months', strtotime($date));

You can then use the new timestamp with the php date() function to display the new date how you wish, for example:

echo date("Y-m-d",$new_timestamp);

For me this way is much better because the code is more readable.

$datetime = Datetime::createFromFormat('Y-m-d', "2011-08-28");
$datetime->modify('-3 months');
echo $datetime->format('Y-m-d');

edit: I'm an idiot. You could do the above as follows

$datetime = new Datetime("2011-08-28");
$datetime->modify('-3 months');
echo $datetime->format('Y-m-d');

edit: As pointed out by calumbrodie, you can use the sub method instead of inverting the interval and adding it to the date object


I was trying to do something similar to the original question. I needed to subtract 1 day from a DateTime object. I know the other solutions work, but here's another way I liked better. I used this function:

function getPreviousDay($dateObject){
    $interval = new DateInterval('P1D');
    $dateObject->sub($interval);
    return $dateObject;
}

$dateObject is a DateTime object that can have any date you wan't, but as I wanted the current date, I wrote:

$dateObject = new DateTime('now');

What my function does, is subtract 1 day from the date it receives, but you can modify it so it subtracts 3 months modifying the DateInterval constructor like this:

$interval = new DateInterval('P3M');
$dateObject->sub($interval);
return $dateObject;

You can find the explanation on the string format used in the DateInterval constructor here

DateInterval constructor documentation

There you'll se that letter 'P' (period) is mandatory, the you use an int to specify period length and then specify the period type, in the case of months 'M'. It looks as if there was an error in the documentation, it says that "M" is used for months and minutes, but it actually works for months. If you need to use minutes, you must specify "PTM", for example "PT3M" for 3 minutes, outside the table it says you must use the "T" identifier for time intervals.

edit: To give a complete example, you have to use this format for a full date time interval:

$format = 'P1Y2M4DT2H1M40S';

This will represent an interval of 1 year, 2 months, 4 days, 2 hours,1 minute and 40 seconds

Hope this helps someone