Link to home
Create AccountLog in
PHP

PHP

--

Questions

--

Followers

Top Experts

Avatar of tonelm54
tonelm54

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

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of ste5anste5an🇩🇪

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?

Avatar of tonelm54tonelm54

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
Avatar of Ryan ChongRyan Chong🇸🇬

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of ste5anste5an🇩🇪

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


Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


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

Avatar of Ryan ChongRyan 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


User generated image
PHP

PHP

--

Questions

--

Followers

Top Experts

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.