Avatar of tonelm54
tonelm54
 asked on

Add months to date

I have a simple issue, where I need to add a number of months to a date, the input date format I cannot change, it will always be dd/mm/YYYY (eg 27/04/2021) as its coming from a html5 input control of date.

So, I generated my code which I thought would work according to several examples Ive found on the web:-
<?php
$inputDate = "27/04/2021";
$noMonths2Add = 3;
$effectiveDate = strtotime("+".$noMonths2Add." months",strtotime($inputDate));
echo json_encode(array("newDate"=>date('Y-m-d',$effectiveDate)));

Open in new window

And it spits out {"newDate":"1970-01-31"} - Which is NOT correct

Any ideas of what Ive done wrong?

Sorry, I could spend ages on this trying to figure out, but not had time to play with this, so probably a really simple issue :-(

Thank you in advance though
PHP

Avatar of undefined
Last Comment
Ryan Chong

8/22/2022 - Mon
ste5an

Your strtotime() usage in line 4 is pretty creative. The add part belongs to the end.

$effectiveDate = strtotime($inputDate."+".$noMonths2Add." months");

Open in new window

But why not using DateTime instead?
tonelm54

ASKER
I have looked at using DateTime in OOP, but it seems a bit too complicated for what I thought was a simple solution :-S

I tried replacing your line in my code:-
<?php

$inputDate = "27/04/2021";
$noMonths2Add =3;
$effectiveDate = strtotime($inputDate."+".$noMonths2Add." months");

echo json_encode(array("newDate"=>date('Y-m-d',$effectiveDate)));

Open in new window

Which still gives the wrong date :-( :-
{"newDate":"1969-12-31"}
ASKER CERTIFIED SOLUTION
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ste5an

The 1970 in the first case and now the 1969, means that some parsing problem or error happens and it fails back to epoch.

So look at the output of your format and the result of the date calculus.

<?php
$inputDate = "27/04/2021";
$noMonths2Add = 3;
$effectiveDate = strtotime($inputDate."+".$noMonths2Add." months");
$formattedDate = date('Y-m-d',$effectiveDate);
echo $effectiveDate." --- ".$formattedDate." --- ".json_encode(array("newDate"=>$formattedDate));

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
tonelm54

ASKER
When I try with the code:-
$inputDate = "27/04/2021";
$noMonths2Add = 3;

$effectiveDate = DateTime::createFromFormat("d/m/Y", $inputDate)->modify("+".$noMonths2Add." months")->format("Y-m-d"

Open in new window

I get Fatal error: Uncaught Error: Call to a member function modify() on bool

Seems to be when I run:-
DateTime::createFromFormat("d/m/Y", $inputDate);

Open in new window

It returns false :-S
Ryan Chong

make sure you written a valid php code.

this worked for me:

<?php

$inputDate = "27/04/2021";
$noMonths2Add = 3;

$effectiveDate = DateTime::createFromFormat("d/m/Y", $inputDate)->modify("+".$noMonths2Add." months")->format("Y-m-d");

echo $effectiveDate;

?>

Open in new window