Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding three months to a date 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.

like image 299
user979331 Avatar asked Mar 26 '12 15:03

user979331


People also ask

How to add time in date in php?

php $date=strtotime("tomorrow"); echo date("Y-m-d h:i:sa", $date) . "<br>"; $date=strtotime("next Sunday"); echo date("Y-m-d h:i:sa", $date) . "<br>"; $date=strtotime("+3 Months"); echo date("Y-m-d h:i:sa", $date) .

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

"\n"; echo date("M - Y",strtotime("-3 Months")).

How can I get plus date in php?

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

What is Strtotime php?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.


2 Answers

Change it to this will give you the expected format:

$effectiveDate = date('Y-m-d', strtotime("+3 months", strtotime($effectiveDate))); 
like image 61
Tchoupi Avatar answered Sep 26 '22 22:09

Tchoupi


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; 
like image 43
Sadee Avatar answered Sep 22 '22 22:09

Sadee