【问题标题】:Overlapping dates in LaravelLaravel 中的重叠日期
【发布时间】:2018-09-09 09:38:45
【问题描述】:

以前我想得到$starttime 和$endtime 之间的时间段,除以$duration 值。

我们设法让它与这段代码一起工作:

$starttime = '9:00';  // your start time
$endtime = '21:00';  // End time
$duration = '30';  // split by 30 mins

$array_of_time = array ();
$start_time    = strtotime ($starttime); //change to strtotime
$end_time      = strtotime ($endtime); //change to strtotime

$add_mins  = $duration * 60;

while ($start_time <= $end_time) // loop between time
{
   $array_of_time[] = date ("h:i", $start_time);
   $start_time += $add_mins; // to check endtie=me
}

$new_array_of_time = array ();
for($i = 0; $i < count($array_of_time) - 1; $i++)
{
    $new_array_of_time[] = '' . $array_of_time[$i] . ' - ' . $array_of_time[$i + 1];
}

现在的问题是我不想在 appointments 表中显示与日期匹配的时间段。

为了获得与该日期匹配的约会,我做了:

$appointments = Appointment::where('user_id', $user->id)->where('appointment_datetime', $date->toDateString() )->get();

现在这会返回我所有的约会,...在每个约会中,我们都有一个列 appointment_start 和 appointment_end ,女巫的格式是 TIME .

下一步是检查所有约会是否有匹配的时间段......这就是我卡住的地方。感谢您的帮助。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    只需用这个替换代码:

    $starttime = '9:00';  // your start time
    $endtime = '21:00';  // End time
    $duration = '30';  // split by 30 mins
    
    $array_of_time = array ();
    $start_time    = strtotime ($starttime); //change to strtotime
    $end_time      = strtotime ($endtime); //change to strtotime
    
    $add_mins  = $duration * 60; // seconds
    
    while ($start_time <= $end_time) // loop between time
    {
     $array_of_time[] = date ("h:i", $start_time);
     $start_time += $add_mins; // to check endtie=me
    }
    
    // Here I am getting the indexes of the time slot which has appointment
    $indexes_to_be_skipped = array();
    foreach($appointments as $appointment) {
      for($i=0;$i<count($array_of_time); $i++) {
        if($array_of_time[$i] == date ("h:i", strtotime($appointment['appointment_time_start']))) {
          $indexes_to_be_skipped[$i] = $i;
        }
      }
    }
    
    $new_array_of_time = array ();
    for($i = 0; $i < count($array_of_time) - 1; $i++)
    {
      $new_array_of_time[] = '' . $array_of_time[$i] . ' - ' . $array_of_time[$i + 1];
    
      // check if current time slot has already appointment
      if(isset($indexes_to_be_skipped[$i])) {
        // then remove it
        unset($new_array_of_time[$i]);
      }
    }
    
    // resetting index
    $narray_of_time = $new_array_of_time;
    $new_array_of_time = array();
    foreach($narray_of_time as $item) {
      $new_array_of_time[] = $item;
    }
    

    希望对你有帮助!

    【讨论】:

    • 像魅力一样工作
    【解决方案2】:

    这可能会有所帮助

    <?php
    $date = \Carbon\Carbon::now(); // the date you need to check
    $starttime = \Carbon\Carbon::create($date->year, $date->month, $date->day, 9,0);  // your start time
    $endtime = \Carbon\Carbon::create($date->year, $date->month, $date->day, 21,0); // end time
    $add_mins  = 60; // minutes to add to each meeting
    $array_of_time = [];
    $current_time = $starttime->copy();
    
    while($current_time->isBefore($endtime)){
        // check if we have record between current and current + $add_mins
        // I suppose appointment_datetime is a datetime field in database
        if(!Appointment::where('user_id', $user->id)->whereBetween('appointment_datetime', [$current_time, $current_time->copy()->addMinute($add_mins)])->count()){
            $array_of_time[] = $current_time->format('H:i') . ' - ' . $current_time->copy()->addMinute($add_mins)->format('H:i');
        }
        $current_time->addMinute($add_mins);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-15
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      相关资源
      最近更新 更多