1

am trying to write a function to find freetime slots during working hours 8 - 17hrs

can anyone help with an algorithm please

Can anyone read this array using its index

      {
        "starttime": "07:30",
        "endtime": "08:30"
      },
      {
        "starttime": "11:00",
        "endtime": "11:30"
      },
      {
        "starttime": "14:00",
        "endtime": "15:00"
      }
    ]

forexample

for ($i = 0; $i < count($value); $i++) {
            $free = array();
            
            if ($value[$i]["starttime"] > $start_time) {
                $free['starttime'] = $start_time;
                $free['endtime'] = $value[$i]["starttime"];
            } else {
                if ($value[$i]["endtime"] > $start_time) {
                    $free['starttime'] = $value[$i]["endtime"];
                    $free['endtime'] = $value[$i + 1]["starttime"];
                } else {
                    $free['starttime'] = $start_time;
                    $free['endtime'] = $value[$i + 1]["starttime"];
                }
            }
            array_push($freetimes["free"], $freetimes);
        }

this way is not possible its not returning any result

  • Create a function with foreach loop and date_diff should help to get the difference. although the array structure is a little odd, why do you have a "date" field, when it is already being used as a key. all the best!!! https://www.delftstack.com/howto/php/how-to-get-time-difference-in-minutes-in-php/#:~:text=Using%20mathematical%20formula-,Use%20date_diff()%20Function%20to%20Get%20Time%20Difference%20in%20Minutes,using%20the%20date_diff()%20function. – Gobinda Nandi May 20 '22 at 10:41
  • Thanks @GobindaNandi, let me try it out – Robert Kwagala May 20 '22 at 10:51

0 Answers0