Adding three months to a date in PHP
Asked Answered
T

11

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.

Terrence answered 26/3, 2012 at 15:33 Comment(6)
What does "didn't work" mean?Supersession
I'm getting 1340649000 as answer, which seems to be correct.Copyboy
Are you sure $effectiveDate stores what you think it stores? It works for me.Brent
I am expecting that date to 2012-06-26 adding 3 months to 2012-03-26Terrence
And date('Y-m-d', 1340661600) gives 2012-06-26 which IS correct.Pursuance
@user1193385 Read up on PHP's strtotime. The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp.Billen
P
236

Change it to this will give you the expected format:

$effectiveDate = date('Y-m-d', strtotime("+3 months", strtotime($effectiveDate)));
Pursuance answered 26/3, 2012 at 15:41 Comment(5)
How to deal with an variable of months? like $months contains the number of months? "+ '$months' months" does not workQuiroz
$offset = 5; echo date('Y-m-d', strtotime("+$offset months", strtotime('2000-01-01'))); DemoSvelte
date() don't use localizations. Better is strftime().Silvern
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.Department
not good solution - does not return correct values - as Dan H mentionedKibe
M
38

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;
Metrical answered 5/6, 2018 at 10:11 Comment(0)
E
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
Emotionality answered 26/3, 2012 at 15:41 Comment(0)
K
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.

Keyser answered 26/3, 2012 at 15:44 Comment(0)
P
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.)

Piton answered 8/12, 2015 at 16:44 Comment(0)
T
3

The following should work,Please Try this:

$effectiveDate = strtotime("+1 months", strtotime(date("y-m-d")));
echo $time = date("y/m/d", $effectiveDate);
Timmerman answered 4/4, 2017 at 7:29 Comment(0)
M
2

Following should work

$d = strtotime("+1 months",strtotime("2015-05-25"));
echo   date("Y-m-d",$d); // This will print **2015-06-25** 
Maziemazlack answered 25/5, 2017 at 10:44 Comment(0)
B
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>';
}
Besetting answered 8/8, 2018 at 10:10 Comment(0)
O
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.).
Offwhite answered 14/12, 2022 at 14:3 Comment(1)
I always forget the P in DateInterval, thanks for posting.Grime
A
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)));
Ariel answered 26/3, 2012 at 15:40 Comment(0)
C
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))); 
}
Careycarfare answered 1/9, 2022 at 16:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.