英文:
How to check if time period is overlapping another time period in PHP Laravel
问题
Sure, here's the translated code:
如何检查数据库中是否已经存在特定用户在给定时间段内的工作小时。因此,当存在与给定日期和时间重叠的工作小时时,该函数应返回true。我尝试过这个,但这不起作用。
function checkExistingWorkingHours($user_id, $start_date, $start_time, $end_date, $end_time)
{
// dd($user_id . " " . $start_date . " " . $start_time . " " . $end_date . " " . $end_time);
$startDateTime = Carbon::parse($start_date . ' ' . $start_time);
$endDateTime = Carbon::parse($end_date . ' ' . $end_time);
$overlapExists = WorkingHour::where('user_id', $user_id)
->where(function ($query) use ($startDateTime, $endDateTime) {
$query->where(function ($query) use ($startDateTime, $endDateTime) {
// 检查开始时间在现有开始时间和结束时间之间是否重叠
$query->where(function ($query) use ($startDateTime, $endDateTime) {
$query->where('start_date', '=', $startDateTime->format('Y-m-d'))
->where('start_time', '<', $endDateTime->format('H:i'))
->where('end_date', '=', $startDateTime->format('Y-m-d'))
->where('end_time', '>', $startDateTime->format('H:i'));
})->orWhere(function ($query) use ($startDateTime, $endDateTime) {
$query->where('start_date', '<', $startDateTime->format('Y-m-d'))
->where('end_date', '=', $startDateTime->format('Y-m-d'))
->where('end_time', '>', $startDateTime->format('H:i'));
});
})->orWhere(function ($query) use ($startDateTime, $endDateTime) {
// 检查结束时间在现有开始时间和结束时间之间是否重叠
$query->where(function ($query) use ($startDateTime, $endDateTime) {
$query->where('start_date', '=', $endDateTime->format('Y-m-d'))
->where('end_date', '>=', $endDateTime->format('Y-m-d'))
->where('start_time', '<', $endDateTime->format('H:i'));
})->orWhere(function ($query) use ($startDateTime, $endDateTime) {
$query->where('start_date', '<=', $startDateTime->format('Y-m-d'))
->where('end_date', '>=', $endDateTime->format('Y-m-d'));
});
});
})->exists();
return $overlapExists;
}
当我使用以下数据测试该函数时,它说存在重叠,即使没有重叠。
checkExistingWorkingHours(1, 2023-06-01, 00:15, 2023-06-01, 03:00);
而在数据库中有一个值是从2023-06-01的03:30到晚上11点。
Please note that the translated code is provided as per your request. If you have any further questions or need assistance with it, feel free to ask.
英文:
How can I check if there is already a working hour in a database for a specific user in a given time slot. So when there is a working hour which is overlapping with the given dates and times, the function should return true. I tried this, but this doesn't work.
function checkExistingWorkingHours($user_id, $start_date, $start_time, $end_date, $end_time)
{
// dd($user_id . " " . $start_date . " " . $start_time . " " . $end_date . " " . $end_time);
$startDateTime = Carbon::parse($start_date . ' ' . $start_time);
$endDateTime = Carbon::parse($end_date . ' ' . $end_time);
$overlapExists = WorkingHour::where('user_id', $user_id)
->where(function ($query) use ($startDateTime, $endDateTime) {
$query->where(function ($query) use ($startDateTime, $endDateTime) {
// Check for overlap where the start_datetime is between existing start_datetime and end_datetime
$query->where(function ($query) use ($startDateTime, $endDateTime) {
$query->where('start_date', '=', $startDateTime->format('Y-m-d'))
->where('start_time', '<', $endDateTime->format('H:i'))
->where('end_date', '=', $startDateTime->format('Y-m-d'))
->where('end_time', '>', $startDateTime->format('H:i'));
})->orWhere(function ($query) use ($startDateTime, $endDateTime) {
$query->where('start_date', '<', $startDateTime->format('Y-m-d'))
->where('end_date', '=', $startDateTime->format('Y-m-d'))
->where('end_time', '>', $startDateTime->format('H:i'));
});
})->orWhere(function ($query) use ($startDateTime, $endDateTime) {
// Check for overlap where the end_datetime is between existing start_datetime and end_datetime
$query->where(function ($query) use ($startDateTime, $endDateTime) {
$query->where('start_date', '=', $endDateTime->format('Y-m-d'))
->where('end_date', '>=', $endDateTime->format('Y-m-d'))
->where('start_time', '<', $endDateTime->format('H:i'));
})->orWhere(function ($query) use ($startDateTime, $endDateTime) {
$query->where('start_date', '<=', $startDateTime->format('Y-m-d'))
->where('end_date', '>=', $endDateTime->format('Y-m-d'));
});
});
})->exists();
return $overlapExists;
}
When I test the function with the following data, it says, that there is an overlapping, even there isn't an overlapping.
checkExistingWorkingHours(1, 2023-06-01, 00:15, 2023-06-01, 03:00);
And in the database is a value 2023-06-01 from 03:30 to 11pm.
答案1
得分: 1
根据geertjanknapen的评论回答:
$period = CarbonPeriod::create("$start_date $start_time", "$end_date $end_time");
$start = Carbon::create("$to_check->start_date $to_check->start_time");
$end = Carbon::create("$to_check->end_date $to_check->end_time");
if ($period->overlaps($start, $end)) {
return redirect()->back()->withInput()->with('error','时间重叠!');
}
英文:
Answer according to the comment of geertjanknapen:
$period = CarbonPeriod::create("$start_date $start_time", "$end_date $end_time");
$start = Carbon::create("$to_check->start_date $to_check->start_time");
$end = Carbon::create("$to_check->end_date $to_check->end_time");
if ($period->overlaps($start, $end)) {
return redirect()->back()->withInput()->with('error',"Time is overlapping!");
}
答案2
得分: 0
尝试使用以下代码:
$start = Carbon::parse($start_date . ' ' . $start_time)->startOfDay();
$end = Carbon::parse($end_date . ' ' . $end_time)->endOfDay();
$overlapExists = WorkingHour::where('user_id', $user_id)->whereBetween('你的列名', [$start, $end])->exists();
英文:
Try to use this
$start = Carbon::parse($start_date . ' ' . $start_time)->stratOfDay();
$end = Carbon::parse($end_date . ' ' . $end_time)->endOfDay();
$overlapExists = WorkingHour::where('user_id', $user_id)->whereBetween('your column',[$start , $end])->exists();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论