1

I'm working on a system that allow the user to set the number of month for their own duration as the deadline for their project. The problem I'm facing now is how do I calculate their duration starting from the day they submitted their project to its deadline.

This is the input where the user can set their month duration:

<label>Duration:</label>
<input type="number" name="duration" placeholder="Number of months" required="">

This is what I wanted to do just to make it clear:

*Let's say the user set 9 month on duration input.

*And since today is september 2021 as I posted this problem. The start of the project will be this month, and the deadline will be on 2022 June.

I already searched something about this and I can't find the right solution or maybe I just don't know the right keyword to search for.

Chepky Pie
  • 60
  • 5

1 Answers1

1

If I understand you correclty you want to add X month to the current date.

You can do something like

$date = new DateTime('now');
$date->modify('+'. $x . ' month'); //x is your input value

See https://www.php.net/manual/en/datetime.modify.php for more information.

Warzulus
  • 46
  • 5
  • Thankyou! you have understand my logic, but your code is having error on me, I just removed the dot ( . ) inside and it worked. – Chepky Pie Sep 29 '21 at 14:47
  • The code propably didn't worked because there was a '+' sign after the 2nd dot which shouldn't be there. I changed it in my answer. You should use dots for concatenation. – Warzulus Sep 30 '21 at 05:49