如何在PHP Laravel中检查时间段是否与另一个时间段重叠

huangapple go评论62阅读模式
英文:

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 . &quot; &quot; . $start_date . &quot; &quot; . $start_time . &quot; &quot; . $end_date . &quot; &quot; . $end_time);
$startDateTime = Carbon::parse($start_date . &#39; &#39; . $start_time);
$endDateTime = Carbon::parse($end_date . &#39; &#39; . $end_time);
$overlapExists = WorkingHour::where(&#39;user_id&#39;, $user_id)
-&gt;where(function ($query) use ($startDateTime, $endDateTime) {
$query-&gt;where(function ($query) use ($startDateTime, $endDateTime) {
// Check for overlap where the start_datetime is between existing start_datetime and end_datetime
$query-&gt;where(function ($query) use ($startDateTime, $endDateTime) {
$query-&gt;where(&#39;start_date&#39;, &#39;=&#39;, $startDateTime-&gt;format(&#39;Y-m-d&#39;))
-&gt;where(&#39;start_time&#39;, &#39;&lt;&#39;, $endDateTime-&gt;format(&#39;H:i&#39;))
-&gt;where(&#39;end_date&#39;, &#39;=&#39;, $startDateTime-&gt;format(&#39;Y-m-d&#39;))
-&gt;where(&#39;end_time&#39;, &#39;&gt;&#39;, $startDateTime-&gt;format(&#39;H:i&#39;));
})-&gt;orWhere(function ($query) use ($startDateTime, $endDateTime) {
$query-&gt;where(&#39;start_date&#39;, &#39;&lt;&#39;, $startDateTime-&gt;format(&#39;Y-m-d&#39;))
-&gt;where(&#39;end_date&#39;, &#39;=&#39;, $startDateTime-&gt;format(&#39;Y-m-d&#39;))
-&gt;where(&#39;end_time&#39;, &#39;&gt;&#39;, $startDateTime-&gt;format(&#39;H:i&#39;));
});
})-&gt;orWhere(function ($query) use ($startDateTime, $endDateTime) {
// Check for overlap where the end_datetime is between existing start_datetime and end_datetime
$query-&gt;where(function ($query) use ($startDateTime, $endDateTime) {
$query-&gt;where(&#39;start_date&#39;, &#39;=&#39;, $endDateTime-&gt;format(&#39;Y-m-d&#39;))
-&gt;where(&#39;end_date&#39;, &#39;&gt;=&#39;, $endDateTime-&gt;format(&#39;Y-m-d&#39;))
-&gt;where(&#39;start_time&#39;, &#39;&lt;&#39;, $endDateTime-&gt;format(&#39;H:i&#39;));
})-&gt;orWhere(function ($query) use ($startDateTime, $endDateTime) {
$query-&gt;where(&#39;start_date&#39;, &#39;&lt;=&#39;, $startDateTime-&gt;format(&#39;Y-m-d&#39;))
-&gt;where(&#39;end_date&#39;, &#39;&gt;=&#39;, $endDateTime-&gt;format(&#39;Y-m-d&#39;));
});
});
})-&gt;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(&quot;$start_date $start_time&quot;, &quot;$end_date $end_time&quot;);
$start = Carbon::create(&quot;$to_check-&gt;start_date $to_check-&gt;start_time&quot;);
$end = Carbon::create(&quot;$to_check-&gt;end_date $to_check-&gt;end_time&quot;);
if ($period-&gt;overlaps($start, $end)) {
return redirect()-&gt;back()-&gt;withInput()-&gt;with(&#39;error&#39;,&quot;Time is overlapping!&quot;);
}

答案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 . &#39; &#39; . $start_time)-&gt;stratOfDay();
$end = Carbon::parse($end_date . &#39; &#39; . $end_time)-&gt;endOfDay();
$overlapExists = WorkingHour::where(&#39;user_id&#39;, $user_id)-&gt;whereBetween(&#39;your column&#39;,[$start , $end])-&gt;exists();

huangapple
  • 本文由 发表于 2023年6月8日 22:09:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定