here's an outline of what the logic would look like -
$interval = 30; // booking/busy interval in minutes
// based on conditional logic in the code, 0 = mon through 6 = sun
define('MON',0); // use defined constants to make code/data self-documenting
// working hours
$hours = [];
$hours[MON] = ["morning_from" => "closed", "morning_to" => "closed", "afternoon_from" => "13:00", "afternoon_to" => "19:00"];
// note: if you define the above array as an array of arrays, with the from/to values, a label (Morning/Afternoon), and a 'closed' or time entry, you can just loop over this to produce the output without needing to have specific code for each possible segment in a day
// busy times - the example data is for a monday
$busy = [];
$busy[] = ["title"=> "Test", "start_date" => "2019-11-25 13:00:00", "end_date" => "2019-11-25 14:00:00"];
// pre-process the booked/busy data
$data = [];
foreach($busy as $arr)
{
$date = $arr['start_date'];
$end_date = $arr['end_date'];
while (strtotime($date) <= strtotime($end_date))
{
$data[$date] = $arr['title'];
$date = date("Y-m-d H:i:s", strtotime("$date +$interval min"));
}
}
// examine the pre-processed booked/busy data
echo '<pre>';
print_r($data);
$current_date = '2019-11-25'; // determine the current date being displayed using whatever logic the code has now
$book_day_int = date('N') - 1; // get the integer day of week number - 1
$current_day = $hours[$book_day_int]; // get the from/to data for the current day
// morning output (see the note above by the $hours array to simplify and make the code general-purpose)
$start = $current_day['morning_from'];
$end = $current_day['morning_to'];
if( $start != 'closed')
{
echo 'Morning<br>';
$start = "$current_date $start:00";
$end = "$current_date $end:00";
$date = $start;
$end_date = $end;
while (strtotime($date) <= strtotime($end_date))
{
echo $date;
// test if this date-time is busy
if(isset($data[$date]))
{
echo ' ' . $data[$date];
}
echo '<br>';
$date = date("Y-m-d H:i:s", strtotime("$date +$interval min"));
}
}
// afternoon output
$start = $current_day['afternoon_from'];
$end = $current_day['afternoon_to'];
if( $start != 'closed')
{
echo 'Afternoon<br>';
$start = "$current_date $start:00";
$end = "$current_date $end:00";
$date = $start;
$end_date = $end;
while (strtotime($date) <= strtotime($end_date))
{
echo $date;
// test if this date-time is busy
if(isset($data[$date]))
{
echo ' ' . $data[$date];
}
echo '<br>';
$date = date("Y-m-d H:i:s", strtotime("$date +$interval min"));
}
}