Chepky Pie
Chepky Pie

Reputation: 60

How to calculate month deadline in php

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.

Upvotes: 1

Views: 49

Answers (1)

Warzulus
Warzulus

Reputation: 46

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.

Upvotes: 1

Related Questions