如何使用 Carbon 计算 2 个日期之间的差异
How to get difference bettwen 2 dates in weeks with Carbon
在 laravel 6 应用程序中,我想获得以周为单位的 2 个日期之间的差异:如果日期打破一周,则获取值 > 0
我看到这里写的 diffInWeeks 方法:https://carbon.nesbot.com/docs/#api-week
我有 2 个约会对象:
$startDate = Carbon::createFromTimestamp(strtotime($eventItem['at_time']));//->format($date_format);
\Log::info('$startDate ::');
\Log::info(print_r($startDate, true));
$offset_x= $startDate->dayOfWeek;
$endDate = $startDate->add($eventItem['duration'] . ' minutes');
\Log::info('-1 $endDate::' . print_r($endDate, true));
$diff_in_weeks= $startDate->diffInWeeks($endDate);
\Log::info('-3 $diff_in_weeks ::' . print_r($diff_in_weeks, true));
在日志中:
[2020-04-20 06:16:56] local.INFO: $startDate ::
[2020-04-20 06:16:56] local.INFO: Carbon\Carbon Object
(
[date] => 2018-02-01 09:08:39.000000
[timezone_type] => 3
[timezone] => UTC
)
[2020-04-20 06:16:56] local.INFO: -1 $endDate::Carbon\Carbon Object
(
[date] => 2018-02-22 04:28:39.000000
[timezone_type] => 3
[timezone] => UTC
)
[2020-04-20 06:16:56] local.INFO: -3 $diff_in_weeks ::0
但在上面的输出中我看到有 21 天的差异 $diff_in_weeks == 0
我尝试替换日期:
$diff_in_weeks= $endDate->diffInWeeks($startDate);
无论如何都得到了 0...
哪种方式有效?
谢谢!
因为一周有 7 天,所以您可以使用 Carbon 的 diffInDays() 方法(查看该期间有多少天)并将结果除以 7,然后您可以将其转换为 int
更新:
正如 'Mohammad Hosseini' 所说,为了避免更改原始值,您可以在 class 中使用 CarbonImmutable,如下所示:
use Carbon\CarbonImmutable;
而不是使用:
use Carbon\Carbon;
而且您无需更改代码中的任何内容
因为当你使用 $endDate = $startDate->add($eventItem['duration'] . ' minutes');
开始日期 ($startDate) 也更改了它的值,从那一行开始,开始日期有了一个新值,实际上开始日期等于结束日期。所以你的解决方案是:
$startDate = Carbon::createFromTimestamp(strtotime($eventItem['at_time']));
$endDate = Carbon::createFromTimestamp(strtotime($eventItem['at_time']))->add($eventItem['duration'] . ' minutes');
$diff_in_weeks= $startDate->diffInWeeks($endDate);
在 laravel 6 应用程序中,我想获得以周为单位的 2 个日期之间的差异:如果日期打破一周,则获取值 > 0
我看到这里写的 diffInWeeks 方法:https://carbon.nesbot.com/docs/#api-week
我有 2 个约会对象:
$startDate = Carbon::createFromTimestamp(strtotime($eventItem['at_time']));//->format($date_format);
\Log::info('$startDate ::');
\Log::info(print_r($startDate, true));
$offset_x= $startDate->dayOfWeek;
$endDate = $startDate->add($eventItem['duration'] . ' minutes');
\Log::info('-1 $endDate::' . print_r($endDate, true));
$diff_in_weeks= $startDate->diffInWeeks($endDate);
\Log::info('-3 $diff_in_weeks ::' . print_r($diff_in_weeks, true));
在日志中:
[2020-04-20 06:16:56] local.INFO: $startDate ::
[2020-04-20 06:16:56] local.INFO: Carbon\Carbon Object
(
[date] => 2018-02-01 09:08:39.000000
[timezone_type] => 3
[timezone] => UTC
)
[2020-04-20 06:16:56] local.INFO: -1 $endDate::Carbon\Carbon Object
(
[date] => 2018-02-22 04:28:39.000000
[timezone_type] => 3
[timezone] => UTC
)
[2020-04-20 06:16:56] local.INFO: -3 $diff_in_weeks ::0
但在上面的输出中我看到有 21 天的差异 $diff_in_weeks == 0
我尝试替换日期:
$diff_in_weeks= $endDate->diffInWeeks($startDate);
无论如何都得到了 0...
哪种方式有效?
谢谢!
因为一周有 7 天,所以您可以使用 Carbon 的 diffInDays() 方法(查看该期间有多少天)并将结果除以 7,然后您可以将其转换为 int
更新: 正如 'Mohammad Hosseini' 所说,为了避免更改原始值,您可以在 class 中使用 CarbonImmutable,如下所示:
use Carbon\CarbonImmutable;
而不是使用:
use Carbon\Carbon;
而且您无需更改代码中的任何内容
因为当你使用 $endDate = $startDate->add($eventItem['duration'] . ' minutes');
开始日期 ($startDate) 也更改了它的值,从那一行开始,开始日期有了一个新值,实际上开始日期等于结束日期。所以你的解决方案是:
$startDate = Carbon::createFromTimestamp(strtotime($eventItem['at_time']));
$endDate = Carbon::createFromTimestamp(strtotime($eventItem['at_time']))->add($eventItem['duration'] . ' minutes');
$diff_in_weeks= $startDate->diffInWeeks($endDate);