110

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.

Pops
  • 30,199
  • 37
  • 136
  • 151
user979331
  • 11,039
  • 73
  • 223
  • 418

11 Answers11

235

Change it to this will give you the expected format:

$effectiveDate = date('Y-m-d', strtotime("+3 months", strtotime($effectiveDate)));
Tchoupi
  • 14,560
  • 5
  • 37
  • 71
  • How to deal with an variable of months? like $months contains the number of months? `"+ '$months' months"` does not work – HKK Apr 24 '14 at 18:09
  • 7
    `$offset = 5; echo date('Y-m-d', strtotime("+$offset months", strtotime('2000-01-01')));` [Demo](http://ideone.com/H8k7lt) – Cees Timmerman Apr 28 '14 at 17:20
  • date() don't use localizations. Better is strftime(). – user706420 Nov 04 '14 at 20:14
  • 5
    That wont work for dates like this: date('2018-01-31', strtotime('+ 3 month')). If the date ends in 31 and the next 3 months doesn't, it will not return the correct date. – Dan H Feb 22 '18 at 12:29
  • 3
    not good solution - does not return correct values - as Dan H mentioned – Josef Vancura Jan 02 '19 at 09:42
36

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;
Sadee
  • 3,010
  • 35
  • 36
6

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
Nick
  • 6,316
  • 2
  • 29
  • 47
4

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.

JohnnyQ
  • 4,839
  • 6
  • 47
  • 65
3

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.)

Community
  • 1
  • 1
gleech
  • 305
  • 4
  • 11
3

The following should work,Please Try this:

$effectiveDate = strtotime("+1 months", strtotime(date("y-m-d")));
echo $time = date("y/m/d", $effectiveDate);
mkl
  • 90,588
  • 15
  • 125
  • 265
2

Following should work

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

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>';
}
1

As of PHP 5.3, DateTime along with DateInterval could be a feasible option to achieve the desired result.

$months = 6; 

$currentDate = new DateTime();

$newDate = $currentDate->add(new DateInterval('P'.$months.'M'));

echo $newDate->format('Y-m-d');

If you want to subtract time from a date, instead of add, use sub.

Here are more examples on how to use DateInterval:

$interval = new DateInterval('P1Y2M3DT4H5M6S');

// This creates an interval of 1 year, 2 months, 3 days, 4 hours, 5 minutes, and 6 seconds.

$interval = new DateInterval('P2W');

// This creates an interval of 2 weeks (which is equivalent to 14 days).

$interval = new DateInterval('PT1H30M');

// This creates an interval of 1 hour and 30 minutes (but no days or years, etc.).
Colandus
  • 1,634
  • 13
  • 21
0
public function getCurrentDate(){
    return date("Y-m-d H:i:s");
}

public function getNextDateAfterMonth($date1,$monthNumber){
   return date('Y-m-d H:i:s', strtotime("+".$monthNumber." months", strtotime($date1))); 
}
cottontail
  • 10,268
  • 18
  • 50
  • 51
yannick
  • 52
  • 2
0

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)));
Brendon Dugan
  • 2,138
  • 7
  • 31
  • 65