首页 > 解决方案 > 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;

}

标签: phplaravel

解决方案


推荐阅读