public function testConvertBlockToTime() { $block = 0; $time = new DateTime(); $time->setTime(8, 0); $tmp = DoctorTime::blockToTime($block); $this->assertEquals(0, $tmp->diff($time)->i); $this->assertEquals(0, $tmp->diff($time)->h); $this->assertEquals(0, $tmp->diff($time)->y); $this->assertEquals(0, $tmp->diff($time)->m); $this->assertEquals(0, $tmp->diff($time)->d); $block = 2; $time = new DateTime(); $time->setTime(8, 30); $tmp = DoctorTime::blockToTime($block); $this->assertEquals(0, $tmp->diff($time)->i); $this->assertEquals(0, $tmp->diff($time)->h); $this->assertEquals(0, $tmp->diff($time)->y); $this->assertEquals(0, $tmp->diff($time)->m); $this->assertEquals(0, $tmp->diff($time)->d); $block = 16; $time = new DateTime(); $time->setTime(12, 0); $tmp = DoctorTime::blockToTime($block); $this->assertEquals(0, $tmp->diff($time)->i); $this->assertEquals(0, $tmp->diff($time)->h); $this->assertEquals(0, $tmp->diff($time)->y); $this->assertEquals(0, $tmp->diff($time)->m); $this->assertEquals(0, $tmp->diff($time)->d); $block = 16; $time = new DateTime(); $time->setTime(12, 0); $basedate = new DateTime(); $basedate->setDate(2010, 10, 10); $tmp = DoctorTime::blockToTime($block, $basedate); $this->assertEquals(0, $tmp->diff($time)->i); $this->assertEquals(0, $tmp->diff($time)->h); $this->assertEquals(0, $tmp->diff($basedate)->y); $this->assertEquals(0, $tmp->diff($basedate)->m); $this->assertEquals(0, $tmp->diff($basedate)->d); }
public static function getFreeSlotByDoctor($doctor_id) { $doctorTimes = DoctorTime::where('doctor_id', $doctor_id)->where('doctorTime_end', '>=', new DateTime())->get(); // echo "doctortimes: \n"; // var_dump($doctorTimes->toArray()); $appointments = Appointment::where('time', '>=', new DateTime())->get(); // echo "appointmnets: \n"; // var_dump($appointments->toArray()); $freeSlots = []; // cerate doctortimes slots foreach ($doctorTimes as $doctorTime) { $beginTime = $doctorTime->doctorTime_begin; $endTime = $doctorTime->doctorTime_end; $datetime = $beginTime; $date = (new DateTime($datetime))->format('Y-m-d'); $freeSlots[$date] = array_fill(0, (20 - 8) * 4, false); $beginBlock = DoctorTime::timeToBlock(new DateTime($beginTime)); $endBlock = DoctorTime::timeToBlock(new DateTime($endTime)); // echo 'beginblock:', $beginBlock, 'endblock:', $endBlock, "\n"; // set as free for ($i = $beginBlock; $i < $endBlock; ++$i) { $freeSlots[$date][$i] = true; } } // echo "freeslots: \n"; // var_dump($freeSlots); // mark out the unfit foreach ($appointments as $appointment) { $datetime = $appointment->time; $date = (new DateTime($datetime))->format('Y-m-d'); $block = DoctorTime::timeToBlock(new DateTime($datetime)); if (isset($freeSlots[$date]) && isset($freeSlots[$date][$block])) { $freeSlots[$date][$block] = false; } } // echo "after freeslots: \n"; // var_dump($freeSlots); // make the result $doctor = HospitalEmployee::find($doctor_id); $result = []; foreach ($freeSlots as $date => $free) { foreach ($free as $i => $isFree) { if ($isFree) { $datetime = DoctorTime::blockToTime($i, new DateTime($date)); $result[] = ['datetime' => $datetime, 'doctor' => $doctor]; } } } return $result; }