0

あり$month1=10ます$month2=4。これらの2か月を合計すると、14になりますが、14か月はありません。つまり、2月/2日です。

6 + 7 = 1の場合のロジックまたはphp関数はありますか?

4

4 に答える 4

5
($month1 + $month2) % 12
                   ^^^

これは剰余演算子であり、最初のオペランドの余りを2番目のオペランドで割った値を返します。

于 2012-12-06T01:31:08.947 に答える
4

別の方法:

$datetime = new DateTime('October');
$datetime->add('4 months');
echo $datetime->format('%m');
于 2012-12-06T01:32:22.807 に答える
3

モジュラス(剰余)を試してください。したがって(6 + 7) / 12、1に等しくなります

http://php.net/manual/en/language.operators.arithmetic.php

于 2012-12-06T01:31:26.457 に答える
1

モジュラスはあなたの質問に直接答えます。ただし、日付演算の場合は、日付固有の関数を使用することをお勧めします:

$date = time();
  // or another unix timestamp
$four_months_later = strtotime("+4 months", $date);
  // still unix timestamp, now 4 months later
于 2012-12-06T01:39:18.160 に答える