Guest User

Untitled

a guest
Jun 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. "availableSlots": [
  2. {
  3. "start": "10:30",
  4. "stop": "11:00"
  5. },
  6. {
  7. "start": "11:00",
  8. "stop": "11:30"
  9. },
  10. {
  11. "start": "11:30",
  12. "stop": "12:00"
  13. },
  14. {
  15. "start": "12:00",
  16. "stop": "12:30"
  17. },
  18. {
  19. "start": "12:30",
  20. "stop": "13:00"
  21. },
  22. {
  23. "start": "13:00",
  24. "stop": "13:30"
  25. },
  26. {
  27. "start": "13:30",
  28. "stop": "14:00"
  29. },
  30. {
  31. "start": "14:00",
  32. "stop": "14:30"
  33. },
  34. {
  35. "start": "14:30",
  36. "stop": "15:00"
  37. },
  38. {
  39. "start": "15:00",
  40. "stop": "15:30"
  41. }]
  42.  
  43. Public function availableTimeSlots($start_time, $end_time, $bookedSlots, $timePerSlot) {
  44. $start = DateTime::createFromFormat('Y-m-d H:i:s', $start_time); //create date time objects
  45. $end = DateTime::createFromFormat('Y-m-d H:i:s', $end_time); //create date time objects
  46. $count = 0; //number of slots
  47. $out = array(); //array of slots
  48. for ($i = $start; $i <= $end;) { //for loop
  49. $avoid = false; //booked slot?
  50. $time1 = $i->format('H:i'); //take hour and minute
  51. $i->modify("+" . $timePerSlot . " minutes"); //add time per slot minutes
  52. $time2 = $i->format('H:i'); //take hour and minute
  53. $slot = $time1 . "-" . $time2; //create a format 12:40-13:00 etc
  54. for ($k = 0; $k < sizeof($bookedSlots); $k++) { //if booked hour
  55. if ($bookedSlots[$k] == $slot) { //check
  56. $avoid = true; //yes. booked
  57. }
  58. }
  59. if (!$avoid && $i <= $end) { //if not booked and less than end time
  60. $count++; //add count
  61. $slots = ['start' => $time1, 'stop' => $time2]; //add count
  62. array_push($out, $slots); //add slot to array
  63. }
  64. }
  65. return $out;
  66.  
  67. }
Add Comment
Please, Sign In to add comment