之間的差異計算函數我的目標是計算包括開始月份在內的兩個日期之間的月數。日期時間(月)
我正在使用此函數進行計算。
function number_months($date1, $date2){
$begin = new DateTime(date('Y-m-d', strtotime($date1)));
$end = new DateTime(date('Y-m-d', strtotime($date2)));
$diff = $end->diff($begin);
return ($diff->format('%y') * 12) + $diff->format('%m') + 1;
}
在大多數情況下,精品工程,但是當函數的參數,例如:
$date1 = 2015-11-04 00:00:00
$date2 = 2017-02-01 00:00:00
函數返回:
15
應該是16
。我錯過了什麼?我在Stackoverflow上進行過研究,嘗試過提供的代碼的各種實現,但問題仍然存在。
謝謝。
在結果中加1就是16 then,。, –
爲什麼應該是16? ('2015-11';'2015-12';'2016-01';'2016-02';'2016-03';'2016-04';'2016-05';'2016-06';'' 2016-07';'2016-08';'2016-09';'2016-10';'2016-11';'2016-12';'2017-01' is __15__ months) –
爲什麼'$ begin = new DateTime(date('Ym-d',strtotime($ date1)));'?爲什麼不簡單地'$ begin = new DateTime($ date1);'? –