$dates[] = array("date" => "2016-02-18 02:00:00", "duration" => "600"); $dates[] = array("date" => "2016-02-18 02:05:00", "duration" => "300"); $dates[] = array("date" => "2016-02-18 02:10:00", "duration" => "600"); $dates[] = array("date" => "2016-02-18 02:25:00", "duration" => "300"); $dates[] = array("date" => "2016-02-18 02:30:00", "duration" => "600"); function closestDates($array, $date){ foreach($array as $day) $interval[] = abs(strtotime($date["date"]) - strtotime($day["date"])); asort($interval); $closest = key($interval); $alreadyChosen[] = $array[$closest]; return $alreadyChosen; } $returnedDates = closestDates($dates, array("date" => "2016-02-18 02:00:00", "duration" => "600")); print_r($returnedDates); // This returns Array ( [0] => Array ( [date] => 2016-02-18 02:00:00 [duration] => 600 ) )
Как я могу адаптировать вышеуказанную функцию, чтобы иметь возможность проверять массив dates
и нажимать следующую ближайшую дату на alreadyChosen
на основе продолжительности.
$dates = ( '0'=> array("date" => "2016-02-18 02:00:00", "duration" => "600"), '1'=> array("date" => "2016-02-18 02:05:00", "duration" => "300"), '2'=> array("date" => "2016-02-18 02:10:00", "duration" => "600"), '3'=> array("date" => "2016-02-18 02:25:00", "duration" => "300"), '4'=> array("date" => "2016-02-18 02:30:00", "duration" => "600") ); // Expected result to be after the checks: $alreadyChosen = array ( '0'=> array("date" => "2016-02-18 02:00:00", "duration" => "600"), '1'=> array("date" => "2016-02-18 02:10:00", "duration" => "600"), '2'=> array("date" => "2016-02-18 02:25:00", "duration" => "300"), '3'=> array("date" => "2016-02-18 02:30:00", "duration" => "600") );
Если вы хотите получить самую свежую дату на сегодняшний день, что вы входите в функцию, и не даете датам, что уже выбрано, вы можете сделать это так:
function pickMostCloserDate($dateArray, $compareDate, &$pickedDates = array()) { $dates = []; foreach($dateArray as $key => $originalDate){ $date = $date['date']; //compare options if(!in_array($originalDate, $pickedDates)){ $dates[$key] = abs(strtotime($compareDate) - strtotime($date)); } } asort($dates); $dateIndex = array_shift(array_keys($dates)); array_push($pickedDates, $dateArray[$dateIndex]); return $dateArray[$dateIndex]; }
Это вернет самую близкую дату и добавит найденную дату в selectedDates, так что вам действительно нужно:
$pickedDates = []; $closeDate = pickMostCloserDate($arrayWithDates, '2016-05-05 00:00:00', $pickedDates);
echo $ closeDate;
// следующий раунд, чтобы выбрать дату
Надеюсь это поможет. Имейте в виду, что я написал это с головы, нужно исправить.
Я нашел решение, выполнив следующие действия: (Я изменил многое из своего вопроса, чтобы заставить его работать)
$dates[] = array("date" => "2016-02-18 02:00:00", "meeting_id" => "1", "class_id" => "10", "duration" => "600"); $dates[] = array("date" => "2016-02-18 02:10:00", "meeting_id" => "1", "class_id" => "10", "duration" => "600"); $dates[] = array("date" => "2016-02-18 02:20:00", "meeting_id" => "1", "class_id" => "10", "duration" => "600"); $dates[] = array("date" => "2016-02-18 02:30:00", "meeting_id" => "1", "class_id" => "10", "duration" => "600"); $dates[] = array("date" => "2016-02-18 02:10:00", "meeting_id" => "2", "class_id" => "10", "duration" => "600"); $dates[] = array("date" => "2016-02-18 02:25:00", "meeting_id" => "2", "class_id" => "10", "duration" => "300"); $dates[] = array("date" => "2016-02-18 02:30:00", "meeting_id" => "3", "class_id" => "10", "duration" => "600"); $dates[] = array("date" => "2016-02-18 02:40:00", "meeting_id" => "3", "class_id" => "10", "duration" => "600"); $dates[] = array("date" => "2016-02-18 02:30:00", "meeting_id" => "4", "class_id" => "11", "duration" => "600"); $dates[] = array("date" => "2016-02-18 02:40:00", "meeting_id" => "4", "class_id" => "11", "duration" => "600"); $dates[] = array("date" => "2016-02-18 02:50:00", "meeting_id" => "4", "class_id" => "11", "duration" => "600"); $dates[] = array("date" => "2016-02-18 03:00:00", "meeting_id" => "4", "class_id" => "11", "duration" => "600"); $firstDates[] = array("date" => "2016-02-18 02:00:00", "meeting_id" => "1", "class_id" => "10", "duration" => "600"); $firstDates[] = array("date" => "2016-02-18 02:10:00", "meeting_id" => "1", "class_id" => "10", "duration" => "600"); $firstDates[] = array("date" => "2016-02-18 02:20:00", "meeting_id" => "1", "class_id" => "10", "duration" => "600"); $firstDates[] = array("date" => "2016-02-18 02:30:00", "meeting_id" => "1", "class_id" => "10", "duration" => "600"); $children[] = array("id" => 1, "class_id" => "10", "fullname" => "Callum"); $children[] = array("id" => 2, "class_id" => "10", "fullname" => "Daniel"); $children[] = array("id" => 3, "class_id" => "11", "fullname" => "Jake"); function dateExists($array, $child, $date) { if (empty($array)) { return false; } $flag = false; foreach($array as $value) { if ($value['child']['id'] == $child['id'] && $value['meeting']['meeting_id'] === $date['meeting_id']){ return true; } if ($value['meeting']['date'] == $date['date']) { return true; } $start = strtotime($value['meeting']['date']); $end = $start + $value['meeting']['duration']; $ts = strtotime($date['date']); if ($ts > $start && $ts < $end) { $flag = true; break; } } return $flag; } $results = []; function buildDates($dates, $children, $key, $firstDate) { $temp = []; $pickedDates = array(); foreach ($children as $child) { if($firstDate["class_id"] === $child["class_id"]) { $temp[] = array('child' => $child, 'meeting' => $firstDate); $pickedDates[$firstDate['date']] = $temp; } break; } foreach ($dates as $key => $date) { foreach ($children as $child) { if($date["class_id"] === $child["class_id"]) { if (!dateExists($temp, $child, $date)) { $temp[] = array('child' => $child, 'meeting' => $date); $pickedDates[$firstDate['date']] = $temp; } } } } return $pickedDates = array($firstDate["date"] => $temp); } foreach ($firstDates as $date) { $results[] = buildDates($dates, $children, $date); } print_r($results);