如何获取两个Carbon对象之间的分钟差异,忽略日期?

huangapple go评论65阅读模式
英文:

How to get the minute difference between two Carbon objects ignoring the date?

问题

I have a start date

$start = Carbon::parse('2023-03-01 10:00:00');

and an end date

$end = Carbon::parse('2023-03-02 17:00:00');

I want to get the minutes between 10:00:00 and 17:00:00 ignoring the date.

I made this working with

$time_end = $end->toTimeString();
$time_start = $start->toTimeString();
$duration = Carbon::parse($time_end)->diffInMinutes(Carbon::parse($time_start));

but with the toTimeString() I kind of leaving the Carbon object and I feel there must be a better approach.

Another idea I had is:

$duration = $end->diffInMinutes($start) - 24 * 60 * $end->diffInDays($start);
英文:

I have a start date

$start = Carbon::parse('2023-03-01 10:00:00');

and an end date

$end = Carbon::parse('2023-03-02 17:00:00');

I want to get the minutes between 10:00:00 and 17:00:00 ignoring the date.

I made this working with

$time_end = $end->toTimeString();
$time_start = $start->toTimeString();
$duration = Carbon::parse($time_end)->diffInMinutes(Carbon::parse($time_start));

but with the toTimeString() I kind of leaving the Carbon object and I feel there must be a better approach.

Even if I don't find a function like that in the CarbonInterface I still feel this must be accomplished better.

Another idea I had is:

$duration = $end->diffInMinutes($start) - 24 * 60  * $end->diffInDays($start);

答案1

得分: 1

Carbon具有一个名为setDateFrom(DateTime Object)的方法。该方法仅获取参数中的日期部分,时间保持不变。可以使用以下一行代码来解决上述任务:

$duration = $start->setDateFrom($end)->diffInMinutes($end);
英文:

Carbon has a setDateFrom(DateTime Object) method. The method only takes the date from the argument. The time remains unchanged. The above task can be solved with one line of code as follows:

$duration = $start->setDateFrom($end)->diffInMinutes($end);

答案2

得分: 0

请注意:

$duration = Carbon::parse($time_end)->diffInMinutes(Carbon::parse($time_start));

由于代码不会立刻执行,第二个 Carbon::parse() 可能会在第一个之后的几微秒内执行,这可能导致时间从 23:59.999 到 00:00.000 而跨越不同的日期。

因此,最好保留开始日期并参考结束时间:

$time_start = $start->toTimeString();
$startTimeWithEndDate = $end->copy()->modify($time_start);
$duration = $end->diffInMinutes($startTimeWithEndDate);

然后,如果需要,你可以在结束时间小于开始时间时交换差值:

$time_start = $start->toTimeString();
$startTimeWithEndDate = $end->copy()->modify($time_start);

if ($startTimeWithEndDate > $end) {
    $startTimeWithEndDate->addDay();
}

$duration = $end->diffInMinutes($startTimeWithEndDate);
英文:

First note that:

$duration = Carbon::parse($time_end)->diffInMinutes(Carbon::parse($time_start));

As code is not instantly executed, the second Carbon::parse() might happen few microseconds after the first one and this might be 23:59.999 and then 00:00.000 so on a different date.

So you better keep the date from start and refer to end time:

$time_start = $start->toTimeString();
$startTimeWithEndDate = $end->copy()->modify($time_start);
$duration = $end->diffInMinutes($startTimeWithEndDate);

Then here you can (if you need) swap the difference if end time is lower then start time:

$time_start = $start->toTimeString();
$startTimeWithEndDate = $end->copy()->modify($time_start);

if ($startTimeWithEndDate > $end) {
    $startTimeWithEndDate->addDay();
}

$duration = $end->diffInMinutes($startTimeWithEndDate);

huangapple
  • 本文由 发表于 2023年2月27日 18:54:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定