我有一天的休息时间和工作时间,我如何才能找到一天的确切工作时间和休息时间?
上班时间是这样的
以前我只有一个场景,比如7月2日,在这种情况下,我把休息时间转换成小时,然后直接从工作时间中扣除。
$secondsPerHour = 3600;
// Getting break entries
$breakTimeEntries = TimeEntries::select('start_date', 'start_time', 'end_time', 'hour_type')
->where('hour_type', 'break')
->orderBy('start_date')->get();
$breakDurationPerDayArray = array();
// Looping through all the break entries and getting the break entries for each day
foreach($breakTimeEntries as $breakTimeEntry) {
$startDate = $breakTimeEntry->start_date;
$breakStartTime = $breakTimeEntry->start_time;
$breakEndTime = $breakTimeEntry->end_time;
$breakHours = $breakEndTime - $breakStartTime;
$breakHoursInSeconds = $breakHours/$secondsPerHour;
// Adding the break entry for the day to array
$breakTimePerDayArray[$startDate] = $breakHoursInSeconds;
}
// Getting work entries
$breakTimeEntries = TimeEntries::select('start_date', 'start_time', 'end_time', 'hour_type')
->where('hour_type', 'work')
->orderBy('start_date')->get();
$workAndBreakHoursArray = array();
// Looping through all the work entries and getting the actual work entries for each day
foreach($breakTimeEntries as $breakTimeEntry) {
$workStartTime = $workTimeEntry->start_time;
$workEndTime = $workTimeEntry->end_time;
$workHours = $workEndTime - $workStartTime;
$workHoursInSeconds = $breakHours/$secondsPerHour;
$startDate = $breakTimeEntry->start_date;
// Checking wether the start date is in array
if(in_array($startDate, $breakDurationPerDayArray)) {
$breakHoursInSeconds = $breakDurationPerDayArray[$startDate];
$actualWorkedHours = $workHoursInSeconds - $breakHoursInSeconds;
//To get the worked hours in minutes
$actualWorkedHoursInMins = $actualWorkedHours/$secondsPerHour;
//To get the break hours in minutes
$breakHoursInMin = $breakHoursInSeconds/$secondsPerHour;
$workAndBreakHoursArray[$startDate] = ['worked_hours' => $actualWorkedHoursInMins, 'break_hours' => $breakHoursInMin];
} else {
//To get the worked hours in minutes
$workHoursInMins = $workHoursInSeconds/$secondsPerHour;
$workAndBreakHoursArray[$startDate] = ['worked_hours' => $workHoursInMins, 'break_hours' => 0];
}
}
此时此刻我已经没有主意了。任何人都建议最好的处理方法。
预期产量是这样的,以小时为单位
[
[2018-07-02] => [Work => 8, Break =>1],
[2018-07-03] => [Work => 8, Break =>1],
[2018-07-04] => [Work => 8, Break =>1],
[2018-07-05] => [Work => 3.5, Break =>0.5],
[2018-07-06] => [Work => 4.5, Break =>0.5],
[2018-07-09] => [Work => 3.5, Break =>0.5],
[2018-07-10] => [Work => 4.5, Break =>0.5]
]
数据库链接
1条答案
按热度按时间5n0oy7gb1#
你给拉威尔贴了标签,所以我猜你在用它。它包含了碳,它是一个伟大的工具,一切与时间有关。https://carbon.nesbot.com/docs/#difference
edit1:下面是一个示例,假设您规范化了条目:
将打印:
我假设对每天的数据进行分组不会带来太多麻烦:)
edit2:在我做一个工作示例之前,我没有注意到你更改了你的问题,所以请原谅数据没有按你想要的格式显示。再说一次,让它符合你的需要应该是一项简单的任务。