Adding three months to a date in PHP

PhpDateStrtotime

Php Problem Overview


I have a variable called $effectiveDate containing the date 2012-03-26.

I am trying to add three months to this date and have been unsuccessful at it.

Here is what I have tried:

$effectiveDate = strtotime("+3 months", strtotime($effectiveDate));

and

$effectiveDate = strtotime(date("Y-m-d", strtotime($effectiveDate)) . "+3 months");

What am I doing wrong? Neither piece of code worked.

Php Solutions


Solution 1 - Php

Change it to this will give you the expected format:

$effectiveDate = date('Y-m-d', strtotime("+3 months", strtotime($effectiveDate)));

Solution 2 - Php

This answer is not exactly to this question. But I will add this since this question still searchable for how to add/deduct period from date.

$date = new DateTime('now');
$date->modify('+3 month'); // or you can use '-90 day' for deduct
$date = $date->format('Y-m-d h:i:s');
echo $date;

Solution 3 - Php

I assume by "didn't work" you mean that it's giving you a timestamp instead of the formatted date, because you were doing it correctly:

$effectiveDate = strtotime("+3 months", strtotime($effectiveDate)); // returns timestamp
echo date('Y-m-d',$effectiveDate); // formatted version

Solution 4 - Php

You need to convert the date into a readable value. You may use strftime() or date().

Try this:

$effectiveDate = strtotime("+3 months", strtotime($effectiveDate));
$effectiveDate = strftime ( '%Y-%m-%d' , $effectiveDate );
echo $effectiveDate;

This should work. I like using strftime better as it can be used for localization you might want to try it.

Solution 5 - Php

Tchoupi's answer can be made a tad less verbose by concatenating the argument for strtotime() as follows:

$effectiveDate = date('Y-m-d', strtotime($effectiveDate . "+3 months") );

(This relies on magic implementation details, but you can always go have a look at them if you're rightly mistrustful.)

Solution 6 - Php

The following should work,Please Try this:

$effectiveDate = strtotime("+1 months", strtotime(date("y-m-d")));
echo $time = date("y/m/d", $effectiveDate);

Solution 7 - Php

Following should work

$d = strtotime("+1 months",strtotime("2015-05-25"));
echo   date("Y-m-d",$d); // This will print **2015-06-25** 
    

Solution 8 - Php

Add nth Days, months and years

$n = 2;
for ($i = 0; $i <= $n; $i++){
    $d = strtotime("$i days");
    $x = strtotime("$i month");
    $y = strtotime("$i year");
    echo "Dates : ".$dates = date('d M Y', "+$d days");
    echo "<br>";
    echo "Months : ".$months = date('M Y', "+$x months");
    echo '<br>';
    echo "Years : ".$years = date('Y', "+$y years");
    echo '<br>';
}

Solution 9 - Php

The following should work, but you may need to change the format:

echo date('l F jS, Y (m-d-Y)', strtotime('+3 months', strtotime($DateToAdjust)));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser979331View Question on Stackoverflow
Solution 1 - PhpTchoupiView Answer on Stackoverflow
Solution 2 - PhpSadeeView Answer on Stackoverflow
Solution 3 - PhpNickView Answer on Stackoverflow
Solution 4 - PhpJohnnyQView Answer on Stackoverflow
Solution 5 - PhpgleechView Answer on Stackoverflow
Solution 6 - PhpDipak kukadiyaView Answer on Stackoverflow
Solution 7 - PhpRickyView Answer on Stackoverflow
Solution 8 - PhpRahul GandhiView Answer on Stackoverflow
Solution 9 - PhpBrendon DuganView Answer on Stackoverflow