使用函数式方法来创建时间,月份计算差值。
$startStr = '2021-10-01 00:00:00';
$endStr = '2022-01-06 18:00:00';
$start = date_create($startStr);
$end = date_create($endStr);
$diff = $start->diff($end);
$diffMonth = intval($diff->format('%y')) * 12 + intval($diff->format('%m'));
echo $diffMonth;
引自:https://www.cnblogs.com/zwtqf/p/10063949.html