Carbon/PHP涉及2月时diffInMonths返回错误值问题求助
Carbon计算跨2月的月份差出现错误
计算用户创建日期与当前日期的月份差值时,涉及2月的场景会返回错误结果。例如计算2023-02-01 00:00:00到2023-03-01 23:59:59的月份差,预期结果为1,但实际返回0。
代码
$associated_user_created_at = Carbon::parse("2023-02-01 00:00:00"); $currentDate = Carbon::parse("2023-03-01 23:59:59"); error_log("H E L L O "); error_log($associated_user_created_at->format('d F Y')); error_log($currentDate->format('d F Y')); $diffInMonths = $associated_user_created_at->diffInMonths($currentDate, false); // $diffInMonths = $associated_user_created_at->diff($currentDate); error_log("****"); error_log("Diff in months = " .$diffInMonths); dd($diffInMonths);
输出
H E L L O 01 February 2023 01 March 2023 **** Diff in months = 0
已尝试过相关解决方案,但问题仍未解决。
项目规格
- PHP 7.4.25
- Laravel 5
内容的提问来源于stack exchange,提问作者Yeo Bryan
相关产品推荐
相关产品推荐

