我有一张能够记录时间和出勤率的基本表格。
我试图将总时数放在网站上,来自我雄辩的查询:
$attendance = DB::table('staff_attendances')
->whereBetween('in_date', array($date1, $date2))->where('staff_id', $ID)->select('first_name', 'last_name', 'in_date', 'out_date', 'in_time', 'out_time')->get();
我在json中得到以下内容。
[{"first_name":"TestFirst","last_name":"TestLast","in_date":"2016-01-30","out_date":"2016-01-30","in_time":"11:40:34","out_time":"12:41:10"},
{"first_name":"TestFirst","last_name":"TestLast","in_date":"2016-01-30","out_date":"2016-01-30","in_time":"13:02:27","out_time":"14:02:32"}]
哪种方法最适合在网站上放置总时数?
我尝试过使用以下碳:
$startTime = Carbon::parse($attendance->in_time);
$finishTime = Carbon::parse($attendance->out_time);
$totalDuration = $finishTime->diffInHours($startTime);
但是我得到了#34;试图获得非对象的属性"
答案 0 :(得分:2)
Carbon是一个扩展DateTime类的类。它应该处理日期和/或时间。但看起来您的解析调用不会返回Carbon实例。所以我建议你使用完整的日期格式(" Y-m -d H:i:s"),如下例所示:
<?php
$totalDuration = 0;
foreach($attendance as $aAttendance){
$startTime = Carbon::parse($aAttendance->in_date.' '.$aAttendance->in_time);
$finishTime = Carbon::parse($aAttendance->out_date.' '.$aAttendance->out_time);
$totalDuration += intval($finishTime->diffInHours($startTime));
}
?>
使用foreach指令,您将完成所有考勤并在变量$ totalDuration中执行所有diffInHours返回的总和。