アンコールリースと金融コリント:
私は、私のこれまでの12ヶ月を追加します。私の開始日は2020年2月29日であり、私はこれまで12ヶ月を追加します。
コード:
$startdate = '02/29/2020';
date('m/d/Y', strtotime('+12 months', strtotime($startdate)));
出力:
03/01/2021
私は12ヶ月を追加するには、このコードを使用しますが、出力がある2021年3月1日、実際の出力があるべきときには、2020年2月28日。
シャシャンク・シャー:
見て!
function add_months($months, DateTime $dateObject)
{
$next = new DateTime($dateObject->format('Y-m-d'));
$next->modify('last day of +'.$months.' month');
if($dateObject->format('d') > $next->format('d')) {
return $dateObject->diff($next);
} else {
return new DateInterval('P'.$months.'M');
}
}
function getCalculatedDate($d1, $months)
{
$date = new DateTime($d1);
// call second function to add the months
$newDate = $date->add(add_months($months, $date));
//formats final date to m/d/Y form
$dateReturned = $newDate->format('m/d/Y');
return $dateReturned;
}
例は次のようになります -
$startDate = '02/29/2020';
$nMonths = 12; // choose how many months you want to add
$finalDate = getCalculatedDate($startDate, $nMonths); // output: 02/28/2021
この方法であなたはの出力が得られます 02/28/2021