Question: Need help with this function, gives overlapping times and does not appropriately give times appoitnment for new patients: function calculateAppointmentTime ( $isNewPatient, $desiredStartTime, $desiredEndTime )

Need help with this function, gives overlapping times and does not appropriately give times appoitnment for new patients:
function calculateAppointmentTime($isNewPatient, $desiredStartTime, $desiredEndTime){
$startTime = strtotime("09:00");
$endTime = strtotime("18:00");
$appointmentDuration =30*60;
if ($isNewPatient === "Yes"){
$appointmentDuration =60*60;
}
$desiredStartTime = empty($desiredStartTime)? $startTime : strtotime(date('Y-m-d').''. $desiredStartTime);
$desiredEndTime = empty($desiredEndTime)? $endTime : strtotime(date('Y-m-d').''. $desiredEndTime);
if ($desiredStartTime < $startTime || $desiredEndTime > $endTime){
return "* Constraints Violated";
}
$latestEndTime = $startTime;
foreach ($_SESSION['patients'] as $patient){
$patientEndTime = strtotime($patient['Appointment Time'])+ $appointmentDuration;
$latestEndTime = max($latestEndTime, $patientEndTime);
}
$driveTime =($index >0)? calculateDriveTime($_SESSION['patients'][$index -1]['Address Coordinates'], $addressc) : 0;
$appointmentTime = max($desiredStartTime, $latestEndTime)+ $driveTime;
while ($appointmentTime < $desiredEndTime){
$isSlotAvailable = true;
foreach ($_SESSION['patients'] as $patient){
$patientAppointmentTime = strtotime($patient['Appointment Time']);
$patientEndTime = $patientAppointmentTime + $appointmentDuration;
if (
($appointmentTime >= $patientAppointmentTime && $appointmentTime < $patientEndTime)||
($appointmentTime + $appointmentDuration > $patientAppointmentTime && $appointmentTime + $appointmentDuration <= $patientEndTime)
){
$isSlotAvailable = false;
break;
}
}
if ($isSlotAvailable){
if ($appointmentTime + $appointmentDuration <= $desiredEndTime){
break;
}
}
$appointmentTime += $appointmentDuration;
}
return date("g:i a", $appointmentTime);
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!