简体   繁体   中英

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.

你可以试试:

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

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

您可以使用strtotime:

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

在PHP 5.4+

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM