Laravel使用CarbonInterval或Carbon PHP在两个日期之间获得了总时间工人
我有分配给用户的时间变化。假设一个夜班的开始时间是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定它是否会充分解决您的问题,但请查看以下内容:
upd:
在您的情况下,该解决方案可能会起作用,但请确保您已经测试了所有边缘案例:
Not sure if it will fully solve your problem, but check out this :
UPD:
It might be this solution will work in your case, but make sure that you have tested all of the edge-cases: