あり$month1=10
ます$month2=4
。これらの2か月を合計すると、14になりますが、14か月はありません。つまり、2月/2日です。
6 + 7 = 1の場合のロジックまたはphp関数はありますか?
($month1 + $month2) % 12
^^^
これは剰余演算子であり、最初のオペランドの余りを2番目のオペランドで割った値を返します。
別の方法:
$datetime = new DateTime('October');
$datetime->add('4 months');
echo $datetime->format('%m');
モジュラス(剰余)を試してください。したがって(6 + 7) / 12
、1に等しくなります
モジュラスはあなたの質問に直接答えます。ただし、日付演算の場合は、日付固有の関数を使用することをお勧めします:
$date = time();
// or another unix timestamp
$four_months_later = strtotime("+4 months", $date);
// still unix timestamp, now 4 months later