PHP
--
Questions
--
Followers
Top Experts
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:-
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
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)));And it spits out {"newDate":"1970-01-31"} - Which is NOT correctAny 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.
Your strtotime() usage in line 4 is pretty creative. The add part belongs to the end.
$effectiveDate = strtotime($inputDate."+".$noMonths2Add." months");
But why not using DateTime instead?
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:-
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)));Which still gives the wrong date :-( :-{"newDate":"1969-12-31"}
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
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.
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));






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
When I try with the code:-
Seems to be when I run:-
$inputDate = "27/04/2021";
$noMonths2Add = 3;
$effectiveDate = DateTime::createFromFormat("d/m/Y", $inputDate)->modify("+".$noMonths2Add." months")->format("Y-m-d"I get Fatal error: Uncaught Error: Call to a member function modify() on boolSeems to be when I run:-
DateTime::createFromFormat("d/m/Y", $inputDate);It returns false :-S
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.