php - PHP Laravel 获取在 2 个日期之间的数组中找到的每个日期的时间间隔,检查当前时间
问题描述
目前,我正在尝试通过从两个日期之间的日期范围数组中获取数据库表中从 start_time 到 startlunch_timefound 的时间段来创建预订表格。日期数组按诊所在一周内的营业时间过滤,例如诊所在星期一、星期二和星期五营业。我的 start_time、end_time、午餐时间开始和午餐时间结束字段是从我的数据库中检索的,并且是时间格式的。
我希望能够获得当前时间之后的时间间隔数组,例如 30 分钟(1100 - 1130)。我不确定如何包含检查语句 n 在当前时间之后填充正确的时间间隔。
以下是我检索日期范围的方法:
public function getWorkDayInRange($workday, $dateFrom, $dateTo)
{
$holidays = $this->getHolidays();
$leaves = $this->getLeaves();
$startDate = new DateTime($dateFrom);
$endDate = new DateTime($dateTo);
$interval = new DateInterval('P1D');
$dateRange = new DatePeriod($startDate, $interval, $endDate);
$results = [];
foreach ($dateRange as $date) {
$name = $date->format('l');
if (in_array($name, $workday) && !in_array($date->format('Y-m-d'), $holidays) && !in_array($date->format('Y-m-d'), $leaves)) {
$times = $this->getTimeslotBeforeLunch($name);
//Start time of clinic to start of lunch time
foreach($times as $time){
$results[] = $date->format('Y-m-d') . "\r\n" . $time;
}
$timeafters = $this->getTimeslotAfterLunch($name);
//End time of lunch time to End time of clinic
foreach($timeafters as $timeafter){
$results[] = $date->format('Y-m-d') . "\r\n" . $timeafter;
}
}
}
return $results;
}
下面是我根据表格上的 start_time、startlunch_time 和间隔设置获取时间数组的方法:
private function getTimeslotBeforeLunch($workday)
{
$timeslot = WorkingHour::where('clinic_id', $_SESSION['clinic_ID'])->where('off_day', false)->where('day', $workday)->get();
$timeslotArray = json_decode($timeslot, true);
foreach($timeslotArray as $timeslot){
$starttime = strtotime($timeslot['start_time']);
$endtime = strtotime($timeslot['startlunch_time']);
$duration = $timeslot['interval'];
$array_of_time = array();
$add_mins = $duration * 60;
while($starttime <= $endtime)//loop between time
{
$array_of_time[] = date("h:i A", $starttime);
$starttime += $add_mins;
}
$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];
}
}
return $new_array_of_time;
}
解决方案
推荐阅读
- typescript - 如果输入对象具有属性“x”,则将字段“x”添加到类
- c# - C# 控制台应用程序 - 连接到 Oracle 12c
- ibm-cloud - 您好,我目前正在学习 IBM 课程,但我的启用持续交付按钮不起作用。我该怎么办?
- string - 处理字符串格式不加零
- php - PHP 中的 Google FCM 显示 IOS 推送通知错误,但在 Android 上工作
- install4j - Install4j 需要时间来清理自己
- java - org.xml.sax.SAXParseException;尝试从请求中获取 xml 输入时文件过早结束
- node.js - 如何在 NodeJS 中使用带有图表和谷歌地图的 html 生成 PDF
- rust - Rust 中结构字段的多态更新
- android - Facebook 广告活动未显示应用安装数据