Shairyar
Shairyar

Reputation: 3356

convert the month number to date

I am working on a task where I need the user to provide exact number of months it will take them to complete a task and then i need to convert that number to an exact date, so lets suppose a user enters 6, this should give me a date 6 months from now.

I tried the following code looking at different examples on line but I have a feeling the following examples treats the $monthNum as the actual month of a year rather than what I need it to do.

$monthNum = 5;
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
echo $monthName; 

I will really appreciate any assistance here.

Upvotes: 0

Views: 57

Answers (4)

Ali Gajani
Ali Gajani

Reputation: 15091

Demo here

Pop in your month in the modify() method.

$monthNum = 6;

$date = new DateTime();

$date->modify(" +{$monthNum} month");

echo $date->format("Y-m-d");

Outputs

2015-05-14

Upvotes: 1

itachi
itachi

Reputation: 6393

in php 5.4+

echo (new DateTime())->modify('+6 months')->format('d M Y');

Upvotes: 0

Jerodev
Jerodev

Reputation: 33186

You can use strtotime:

$date = date("Y-m-d", strtotime("+5 months"));

Upvotes: 0

k.tarkin
k.tarkin

Reputation: 736

You can try:

$time = new \DateTime('+5 months');

Upvotes: 1

Related Questions