【发布时间】:2021-05-20 15:47:37
【问题描述】:
我有一个开始日期和结束日期的示例,下面是 2020 年 12 月 1 日 - 2021 年 2 月 15 日
$startDate = '2020-12-01'
$endDate = '2021-02-15'
我想要达到的理想结果:
12 月 = 31 天 31 天 = 1.0
一月 = 31 天 / 31 天 = 1.0
二月 = 15 天 / 28 天 = 0.53
总量 = 2.53
我目前的公式:
$endDate->diffInDays($startDate)
77/90 天 * 3 个月 = 2.57
$monthPeriod = CarbonPeriod::create($startDate, $endDate)->month();
$yearPeriod = CarbonPeriod::create($startDate, $endDate)->year();
$months = collect($monthPeriod)->map(function (Carbon $date) {
return [
'month' => $date->month,
'name' => $date->format('F'),
'days' => $date->daysInMonth,
];
});
foreach ($months as $month) {
$quantity = $endDate->diffInDays($startDate)
}
【问题讨论】:
-
您是指 2019 年 12 月吗?
-
我的错误,我已经编辑了我的结束日期
标签: php laravel php-carbon