php - Laravel 中的重叠日期

标签 php laravel

以前我想得到 $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_startappointment_end ,女巫在 TIME 中格式化.

下一步是检查所有预约的匹配时间段……这就是我遇到的问题。感谢您的帮助。

最佳答案

只需将代码替换为以下代码即可:

$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;
}

希望对您有所帮助!

关于php - Laravel 中的重叠日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52243211/

相关文章:

php - Laravel request()->fingerprint() 函数以及加密用户数据记录的可能性?

php - 我的搜索引擎没有显示任何错误,但也没有显示任何结果

Php 插入/更新多行,使用数组而不是 foreach

php - PHP 中类采样的漏洞

php - 组合访问器和修改器逻辑以将自定义属性添加到模型

laravel - 如何更改CRUD自带的默认前缀 "Create/Update"?

Laravel 验证 : difference between numeric and integer?

php - 如何使用 Mink 查找文本节点元素?

php - 如何删除无序列表的起始元素符号

php - 如何从我的网站中删除重定向链错误