如何在 PHP Laravel 中检查时间段是否与另一个时间段重叠
P粉011360903
P粉011360903 2023-09-03 00:18:11
[PHP讨论组]

如何检查数据库中是否已存在给定时间段内特定用户的工作时间。因此,当工作时间与给定日期和时间重叠时,该函数应返回 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) {
            // 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;
}

当我使用以下数据测试该函数时,它说存在重叠,即使没有重叠。

检查现有工作时间(1, 2023-06-01, 00:15, 2023-06-01, 03:00);

数据库中的值是 2023-06-01 03:30 到 11pm。

P粉011360903
P粉011360903

全部回复(2)
P粉515066518

尝试使用这个

$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();
P粉014218124

根据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!");
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号