$date1 = Carbon::parse('2023-11-30');
$date2 = Carbon::parse('2023-10-01');
$months = $date1->diffInMonths($date2, false);
// Check if there are remaining days after calculating the month difference
if ($date1->copy()->subMonths($months)->gt($date2)) {
$months++;
}
echo $months; // Output: 2
2条答案
按热度按时间qhhrdooz1#
如果你想要四舍五入的差异:
64jmpszr2#
当有部分月份时,对Carbon的**diffInMonths()**的结果进行舍入:
这将计算月数差,如果减去月数差后还有剩余天数,则加1。这样,您将得到2作为示例中的结果,而无需手动将1加到结果中。