Laravel使用CarbonInterval或Carbon PHP在两个日期之间获得了总时间工人

发布于 2025-02-12 07:51:14 字数 735 浏览 1 评论 0原文

我有分配给用户的时间变化。假设一个夜班的开始时间是21-00-00 pm的7月,其结束时间为7月2日的03-00-00 AM。现在,我想通过添加等于6小时的终止时间来获得一名员工工作的总时间,我应该得到六个小时。我已经尝试了遵循当前日期的代码,例如,如果开始时间等于15-00-00-00-00 7月1日21-00-00 PM 7月1日,但是当我上面提到的两个日期之间存在偏移时,它将失败。

    $attendance_start_time = \Carbon\Carbon::parse($shift->start_time);

   $attendance_end_time = \Carbon\Carbon::parse($shift->end_time);

   $total_attendance_time=$attendance_end_time->diffInSeconds($attendance_start_time,true);

   \Carbon\CarbonInterval::seconds($total_attendance_time)->cascade()->forHumans()

我期望有六个小时,但它给我以下结果

18 hours

,我需要精确的六个小时

i have time shifts which are assigned to the user. Suppose a night shift starting time is 21-00-00 pm of one july and its ending time is 03-00-00 am of 2nd July. Now i want to get total time a employee worked by adding start time to end time which is equal to 6 hours and i should get six hours. I have tried following code which is working fine for current date like it will give me exact 6 hours if start time is equal to 15-00-00 pm of 1 july to 21-00-00 pm of 1 july but it will fail when shifts exists between two dates as i mentioned above.

    $attendance_start_time = \Carbon\Carbon::parse($shift->start_time);

   $attendance_end_time = \Carbon\Carbon::parse($shift->end_time);

   $total_attendance_time=$attendance_end_time->diffInSeconds($attendance_start_time,true);

   \Carbon\CarbonInterval::seconds($total_attendance_time)->cascade()->forHumans()

i am expecting six hours but it is giving me following result

18 hours

i want exact six hours

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给不了的爱 2025-02-19 07:51:14

不确定它是否会充分解决您的问题,但请查看以下内容:

\Carbon\CarbonInterval::seconds(4100)->cascade()->forHumans(['aUnit' => true]);

upd:
在您的情况下,该解决方案可能会起作用,但请确保您已经测试了所有边缘案例:

    $startTime = \Carbon\Carbon::parse('2022-07-02 19:00');

    $endTime = \Carbon\Carbon::parse('2022-07-02 19:30');

    $diff = $startTime->diffInSeconds($endTime);

    if ($endTime->greaterThanOrEqualTo($endTime) && ! $endTime->isSameDay($startTime)) {
        $diff = $startTime->diffInSeconds($endTime->addDay());
    }

    $humanReadable = \Carbon\CarbonInterval::seconds($diff)->cascade()->forHumans(['aUnit' => true]);

Not sure if it will fully solve your problem, but check out this :

\Carbon\CarbonInterval::seconds(4100)->cascade()->forHumans(['aUnit' => true]);

UPD:
It might be this solution will work in your case, but make sure that you have tested all of the edge-cases:

    $startTime = \Carbon\Carbon::parse('2022-07-02 19:00');

    $endTime = \Carbon\Carbon::parse('2022-07-02 19:30');

    $diff = $startTime->diffInSeconds($endTime);

    if ($endTime->greaterThanOrEqualTo($endTime) && ! $endTime->isSameDay($startTime)) {
        $diff = $startTime->diffInSeconds($endTime->addDay());
    }

    $humanReadable = \Carbon\CarbonInterval::seconds($diff)->cascade()->forHumans(['aUnit' => true]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文