以前,我想获取$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 = Appointment::where('user_id', $user->id)->where('appointment_datetime', $date->toDateString() )->get();
现在,这将返回所有约会,...在每个约会中,我们都有一列 appointment_start 和 appointment_end ,女巫的格式为 TIME 。
下一步将是检查所有匹配时隙的约会...这就是我遇到的问题。谢谢您的帮助。
答案 0 :(得分:3)
只需用以下代码替换代码:
$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;
}
希望对您有所帮助!
答案 1 :(得分:0)
这可能会帮助
<?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);
}