【发布时间】:2020-01-30 10:47:56
【问题描述】:
我有签入时间和签出时间,我已经有了动态时间段的代码,但我正在努力处理动态时间间隔。下面的代码 -
$attendance = Attendance::where('user_id', '=', $request->user()->id)->latest()->first();
if(!is_null($attendance) && !is_null($attendance->checkout)){
$created = (string) $attendance->created_at;
$timing = array($created);
$main_diff = Carbon::parse($created)->diffInSeconds($attendance->checkout, false);
$modul = $main_diff % 3600;
$main_amount = $main_diff - $modul;
$count = $main_amount/3600;
if($count > 1){
for($x=0; $x <= count($timing); $x++){
$diff = Carbon::parse($timing[$x])->diffInSeconds($attendance->checkout, false);
if($diff > 0){
if($x==0){
array_push($timing, (string) Carbon::parse($timing[$x])->minute(0)->second(0)->addHour());
}
elseif($x>0 and $x<$count){
$var = Carbon::parse($timing[$x]);
array_push($timing, (string) $var->addHour());
}
elseif($x == $count){
array_push($timing, (string) $attendance->checkout);
}
}
else{
break;
}
}
$timings = [];
$last;
foreach($timing as $edit){
if(isset($last)){
$timings[] = $last.' - '.Carbon::parse($edit)->format('h:i');
}
$last = Carbon::parse($edit)->format('h:i');
}
$variable['data'] = $timings;
return response(json_encode($variable), 200);
}
}
以上代码的输出-
10:15-11:00
11:00-12:00
12:00-12:18
现在有两个不同的事件 EventFirst,EventSecond - 我想要的是将事件的时间整合到主要时间中。 例如 -
10:15 - 11:00
11:00 - EventFirst->start_time
EventFirst->start_time - EventFirst->end_time
EventFirst->end_time - Next Nearest Hour
Next Nearest Hour - EventSecond->start_time
EventSecond->start_time - EventSecond->end_time
EventSecond->end_time - Next Nearest Hour
Next Nearest Hour - Checkout Time
问题在于事件不是实例,而是从数据库中检索的集合。 所以我要做的是首先从一个数组中的事件中获取所有时间,并检查每次迭代。
我不认识,我的大脑已经熟了。
更好的解释
我有三张表,分别是-
出席,活动一,活动二
现在每个表中的相关列-
出席
|____checkout_time(碳时间戳)
|____created_at(碳时间戳)
活动一
|____event_start(碳时间戳)
|____event_duration(整数(秒))
事件二
|____event_time(碳时间戳)
|____event_end_time(碳时间戳)
现在主要要求是,基于出勤表应该有每小时的间隔,除了签到和结帐时间,所以格式应该是 -
checkin - Next Nearest Hour (e.g - 10:15-11:00)
Last Hour - Next Hour (e.g 11:00-12:00, 12:00-13:00, etc)
Nearest Hour Before Checkout - checkout (e.g - 18:00-18:22)
上面的输出已经实现了。
现在,当我必须在主时隙中添加其他两个事件的时隙时,问题就出现了。
所以格式现在变成了 -
checkin - Nearest Hour (e.g 11:34 - 12:00)
(Assuming Event one exists)
(case 1) -
If eventOne start and end is between Nearest Hour and the Next Hour add it in between
(e.g - 12:00-12:06, **12:06-12:54**, 12:54-13:00)
(case 2) -
If eventOne end crosses the Next Hour, unset the normal interval
(e.g - 12:00-12:06, **12:06-13:28**, 13:28-14:00)
Same Goes for eventTwo
现在一切都清楚了吗?
【问题讨论】:
-
您能提供更多信息吗?一般来说,变量和代码不是很容易解释。
标签: php laravel loops object php-carbon