How to get date after 3 months in php?

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.

How to get date after 3 months in php?

Pops

29.5k36 gold badges133 silver badges151 bronze badges

asked Mar 26, 2012 at 15:33

user979331user979331

9,48765 gold badges213 silver badges392 bronze badges

6

Change it to this will give you the expected format:

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

answered Mar 26, 2012 at 15:41

5

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;

answered Jun 5, 2018 at 10:11

How to get date after 3 months in php?

SadeeSadee

2,66432 silver badges35 bronze badges

0

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

answered Mar 26, 2012 at 15:41

How to get date after 3 months in php?

NickNick

6,2282 gold badges28 silver badges46 bronze badges

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.

answered Mar 26, 2012 at 15:44

How to get date after 3 months in php?

JohnnyQJohnnyQ

4,5476 gold badges44 silver badges64 bronze badges

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

answered Dec 8, 2015 at 16:44

How to get date after 3 months in php?

gleechgleech

3054 silver badges11 bronze badges

The following should work,Please Try this:

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

How to get date after 3 months in php?

mkl

86.5k15 gold badges124 silver badges249 bronze badges

answered Apr 4, 2017 at 7:29

Following should work

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

answered May 25, 2017 at 10:44

RickyRicky

1351 silver badge8 bronze badges

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 "
"; echo "Months : ".$months = date('M Y', "+$x months"); echo '
'; echo "Years : ".$years = date('Y', "+$y years"); echo '
'; }

answered Aug 8, 2018 at 10:10

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

answered Mar 26, 2012 at 15:40

How to get date after 3 months in php?

Brendon DuganBrendon Dugan

2,1107 gold badges32 silver badges63 bronze badges

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))); 
}

How to get date after 3 months in php?

answered Sep 1 at 16:14

How to add 1 month in date in php?

php $dt = strtotime("2012-12-21"); echo date("Y-m-d", strtotime("+1 month", $dt)).

Which function can be used to change the date from current month to three months previous?

You can use the EDATE function to quickly add or subtract months from a date. The EDATE function requires two arguments: the start date and the number of months that you want to add or subtract. To subtract months, enter a negative number as the second argument. For example, =EDATE("9/15/19",-5) returns 4/15/19.

What is the use of date() function in php?

The date() function formats a local date and time, and returns the formatted date string.

How can I get plus date in php?

The date_add() function adds some days, months, years, hours, minutes, and seconds to a date.