Jump to content

Compare two array date and create a div


Fratozzi

Recommended Posts

I'm going crazy. I have two arrays.

The first has working hours and the array is like this:

```array(7) { [0]=> array(4) { ["morning_from"]=> string(6) "closed" ["morning_to"]=> string(6) "closed" ["afternoon_from"]=> string(5) "13:00" ["afternoon_to"]=> string(5) "19:00" }```

The second one is the busy times, the array is like this:

```array(2) { [0]=> { ["title"]=> string(13) "Test" ["start_date"]=> string(19) "2019-11-25 13:00:00" ["end_date"]=> string(19) "2019-11-25 14:00:00" } }```

Now I do a cycle of 15 times because I need to make the list for the next 15 days from today.
Then I compare the working hours in the morning with the appointments, if the time is occupied by an appointment I add a class to the div.
The HTML that comes out should look like this:

```
<ul>
  <li>
    <div>
      <p>Sun</p><p>24 Nov</p>
    </div>
    <div class="uk-book-a-visit-closed">Closed</div>
  </li>
  <li>
    <div>
      <p>Mon</p>
      <p>25 Nov</p>
    </div>
    <div>
    <a class="uk-button-book-a-visit-hour uk-button-book-a-visit-hour-busy" id="a-book-a-visit" data-book-day="2019-11-25" data-book-hour="13:00">13:00</a>
    <a class="uk-button-book-a-visit-hour uk-button-book-a-visit-hour-busy" id="a-book-a-visit" data-book-day="2019-11-25" data-book-hour="13:30">13:30</a>
    <a class="uk-button-book-a-visit-hour uk-button-book-a-visit-hour-busy" id="a-book-a-visit" data-book-day="2019-11-25" data-book-hour="14:00">14:00</a>
    <a class="uk-button-book-a-visit-hour" id="a-book-a-visit" data-book-day="2019-11-25" data-book-hour="14:30">14:30</a>
    <a class="uk-button-book-a-visit-hour" id="a-book-a-visit" data-book-day="2019-11-25" data-book-hour="15:00">15:00</a>
    </div>
  </li>
</ul>
```

Unfortunately it does not work as I would like, it does not mark correctly if the date is occupied. I'll post a piece of code:


```for ($book_i=0; $book_i <= 15; $book_i++) {

    if($book_i == 0){
        $book_today_day = date('Y-m-d', strtotime($book_today));
    }else{
        $book_today_day = date('Y-m-d', strtotime($book_today . ' +'.$book_i.' day'));
    }

    $book_day = ucwords(strftime('%d %h', strtotime($book_today_day)));
    $book_name_day = ucwords(strftime('%a', strtotime($book_today_day)));

    if(count($getListCalendarStartToday) > $count_list_calendar_p){
        $day_appointment_p_start = date('Y-m-d', strtotime($getListCalendarStartToday[$count_list_calendar_p]["start_date"]));
        $day_appointment_p_end = date('Y-m-d', strtotime($getListCalendarStartToday[$count_list_calendar_p]["end_date"]));
    }else{
        $day_appointment_p_start = "N.D.";
        $day_appointment_p_end = "N.D.";
    }
    
    if($day_appointment_p_start == $book_today_day){

        $count_more_appointment_on_some_days_morning = 0;
        $count_more_appointment_on_some_days_afternoon = 0;

        foreach ($getListCalendarStartToday as $key => $value) {
            $day = date('Y-m-d', strtotime($value["start_date"]));

            if($day == $book_today_day){
                $count_more_appointment_on_some_days_morning += 1;
                $count_more_appointment_on_some_days_afternoon += 1;
            }
        }

        $book_output .= "<li>";

        if(isset($user["working_time"]) && $user["working_time"]){

            $book_output .= "<div><p>".$book_name_day."</p><p>".$book_day."</p></div>";

                if($book_name_day == $lang["days-mon"]){
                    $book_day_int = 0;
                }else if($book_name_day == $lang["days-tue"]){
                    $book_day_int = 1;
                }else if($book_name_day == $lang["days-wed"]){
                    $book_day_int = 2;
                }else if($book_name_day == $lang["days-thu"]){
                    $book_day_int = 3;
                }else if($book_name_day == $lang["days-fri"]){
                    $book_day_int = 4;
                }else if($book_name_day == $lang["days-sat"]){
                    $book_day_int = 5;
                }else if($book_name_day == $lang["days-sun"]){
                    $book_day_int = 6;
                }

                $book_morning_from = $book_array_working_time[$book_day_int]["morning_from"];
                $book_morning_to = $book_array_working_time[$book_day_int]["morning_to"];
                $book_afternoon_from = $book_array_working_time[$book_day_int]["afternoon_from"];
                $book_afternoon_to = $book_array_working_time[$book_day_int]["afternoon_to"];

                $book_morning_from_hour = (int)substr($book_morning_from, 0, 2);
                $book_morning_to_hour = (int)substr($book_morning_to, 0, 2);
                $book_morning_from_min = (int)substr($book_morning_from, 3, 5);
                $book_morning_to_min = (int)substr($book_morning_to, 3, 5);

                $book_afternoon_from_hour = (int)substr($book_afternoon_from, 0, 2);
                $book_afternoon_to_hour = (int)substr($book_afternoon_to, 0, 2);
                $book_afternoon_from_min = (int)substr($book_afternoon_from, 3, 5);
                $book_afternoon_to_min = (int)substr($book_afternoon_to, 3, 5);

                $book_morning_from_hour_duplicated = $book_morning_from_hour;
                $book_morning_to_hour_duplicated = $book_morning_to_hour;

                $book_afternoon_from_hour_duplicated = $book_afternoon_from_hour;
                $book_afternoon_to_hour_duplicated = $book_afternoon_to_hour;

                $book_check_closed = 0;
                $check_day_morning = 0;

                if($book_afternoon_from_hour == 0 && $book_afternoon_to_hour == 0){
                    if($book_afternoon_from_hour == 0 && $book_afternoon_to_hour == 0 && $book_check_closed == 0){
                        $book_output .= "<div class='uk-book-a-visit-closed'>".$lang["system-appointment-book-a-visit-closed"]."</div>";
                        $book_check_closed = 1;
                    }
                }else{

                    $book_output .= "<div>";

                    for ($n=0; $n < $count_more_appointment_on_some_days_afternoon; $n++) {
                        
                        if($count_list_calendar_p === $count_more_appointment_on_some_days_afternoon && $count_more_appointment_on_some_days_afternoon > 1){
                            $count_list_calendar_p -= 1;
                        }

                        $day_appointment_p_start_hour = date('H', strtotime($getListCalendarStartToday[$count_list_calendar_p]["start_date"]));
                        $day_appointment_p_end_hour = date('H', strtotime($getListCalendarStartToday[$count_list_calendar_p]["end_date"]));
                        $day_appointment_p_start_min = date('i', strtotime($getListCalendarStartToday[$count_list_calendar_p]["start_date"]));
                        $day_appointment_p_end_min = date('i', strtotime($getListCalendarStartToday[$count_list_calendar_p]["end_date"]));
                        
                        for($book_afternoon_from_hour; $book_afternoon_from_hour <= $book_afternoon_to_hour; $book_afternoon_from_hour++){

                            if($book_afternoon_from_hour_duplicated != $book_afternoon_from_hour){
                                continue;
                            }

                            if($book_afternoon_from_hour != $book_afternoon_to_hour){
                                if($day_appointment_p_start_hour == number_add_leading_0($book_afternoon_from_hour)){

                                    $book_afternoon_from_hour_duplicated += 1;
                                    $count_list_calendar_p += 1;

                                    if($book_afternoon_from_min == 0){

                                        if($day_appointment_p_start_hour == $day_appointment_p_end_hour){
                                            
                                            if($day_appointment_p_start_min >= 0 && $day_appointment_p_start_min <= 30){
                                                $book_output .= "<a class='uk-button-book-a-visit-hour uk-button-book-a-visit-hour-busy' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($day_appointment_p_start_hour).":00'>".number_add_leading_0($day_appointment_p_start_hour).":00</a>";
                                                $book_output .= "<a class='uk-button-book-a-visit-hour' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($day_appointment_p_start_hour).":30'>".number_add_leading_0($day_appointment_p_start_hour).":30</a>";
                                            }else{
                                                $book_output .= "<a class='uk-button-book-a-visit-hour' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($day_appointment_p_start_hour).":00'>".number_add_leading_0($day_appointment_p_start_hour).":00</a>";
                                                $book_output .= "<a class='uk-button-book-a-visit-hour uk-button-book-a-visit-hour-busy' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($day_appointment_p_start_hour).":30'>".number_add_leading_0($day_appointment_p_start_hour).":30</a>";
                                            }
                                        }else{

                                            $count_hour_appointment = 0;
                                            
                                            for($day_appointment_p_start_hour; $day_appointment_p_start_hour <= $day_appointment_p_end_hour; $day_appointment_p_start_hour++) {                                                                                     
                                                if($day_appointment_p_start_hour == $day_appointment_p_end_hour){
                                                    if($day_appointment_p_end_min >= 0 && $day_appointment_p_end_min <= 30){
                                                        $book_output .= "<a class='uk-button-book-a-visit-hour uk-button-book-a-visit-hour-busy' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($day_appointment_p_start_hour).":00'>".number_add_leading_0($day_appointment_p_start_hour).":00</a>";
                                                        $book_output .= "<a class='uk-button-book-a-visit-hour' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($day_appointment_p_start_hour).":30'>".number_add_leading_0($day_appointment_p_start_hour).":30</a>";
                                                    }else{
                                                        $book_output .= "<a class='uk-button-book-a-visit-hour' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($day_appointment_p_start_hour).":00'>".number_add_leading_0($day_appointment_p_start_hour).":00</a>";
                                                        $book_output .= "<a class='uk-button-book-a-visit-hour uk-button-book-a-visit-hour-busy' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($day_appointment_p_start_hour).":30'>".number_add_leading_0($day_appointment_p_start_hour).":30</a>";
                                                    }
                                                }else{
                                                    if($day_appointment_p_start_min >= 0){
                                                        $book_output .= "<a class='uk-button-book-a-visit-hour uk-button-book-a-visit-hour-busy' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($day_appointment_p_start_hour).":00'>".number_add_leading_0($day_appointment_p_start_hour).":00</a>";
                                                        $book_output .= "<a class='uk-button-book-a-visit-hour uk-button-book-a-visit-hour-busy' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($day_appointment_p_start_hour).":30'>".number_add_leading_0($day_appointment_p_start_hour).":30</a>";
                                                    }else{
                                                        $book_output .= "<a class='uk-button-book-a-visit-hour uk-button-book-a-visit-hour-busy' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($day_appointment_p_start_hour).":30'>".number_add_leading_0($day_appointment_p_start_hour).":30</a>";
                                                    }

                                                    $count_hour_appointment += 1;
                                                }
                                                
                                            }

                                            $book_afternoon_from_hour_duplicated += $count_hour_appointment;
                                            $count_list_calendar_p += 1;
                                            
                                        }

                                        break;

                                    }else{

                                        if($book_i == 0){

                                            if($day_appointment_p_start_min >= $book_afternoon_from_min && $day_appointment_p_start_min <= 30){
                                                $book_output .= "<a class='uk-button-book-a-visit-hour uk-button-book-a-visit-hour-busy' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($day_appointment_p_start_hour).":".number_add_leading_0($book_afternoon_from_min)."'>".number_add_leading_0($day_appointment_p_start_hour).":".number_add_leading_0($book_afternoon_from_min)."</a>";
                                            }else{
                                                $book_output .= "<a class='uk-button-book-a-visit-hour' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($book_afternoon_from_hour).":".number_add_leading_0($book_afternoon_from_min)."'>".number_add_leading_0($book_afternoon_from_hour).":".number_add_leading_0($book_afternoon_from_min)."</a>";
                                                $book_output .= "<a class='uk-button-book-a-visit-hour uk-button-book-a-visit-hour-busy' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($day_appointment_p_start_hour).":30'>".number_add_leading_0($day_appointment_p_start_hour).":30</a>";
                                            }

                                        }else{

                                            if($day_appointment_p_start_min >= $book_afternoon_from_min && $day_appointment_p_start_min >= 30){
                                                $book_output .= "<a class='uk-button-book-a-visit-hour uk-button-book-a-visit-hour-busy' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($day_appointment_p_start_hour).":30'>".number_add_leading_0($day_appointment_p_start_hour).":30</a>";
                                            }else{
                                                $book_output .= "<a class='uk-button-book-a-visit-hour uk-button-book-a-visit-hour-busy' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($book_afternoon_from_hour).":00'>".number_add_leading_0($book_afternoon_from_hour).":00</a>";
                                                $book_output .= "<a class='uk-button-book-a-visit-hour' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($day_appointment_p_start_hour).":30'>".number_add_leading_0($day_appointment_p_start_hour).":30</a>";
                                            }

                                        }

                                        $book_output .= "<a class='uk-button-book-a-visit-hour' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($book_afternoon_from_hour).":00'>".number_add_leading_0($book_afternoon_from_hour).":00</a>";
                                    }

                                }else{

                                    $book_afternoon_from_hour_duplicated += 1;
                                    $count_list_calendar_p += 1;

                                    if($book_afternoon_from_min == 0){
                                        $book_output .= "<a class='uk-button-book-a-visit-hour' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($book_afternoon_from_hour).":00'>".number_add_leading_0($book_afternoon_from_hour).":00</a>";
                                        $book_output .= "<a class='uk-button-book-a-visit-hour' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($book_afternoon_from_hour).":30'>".number_add_leading_0($book_afternoon_from_hour).":30</a>";
                                    }else{
                                        if($book_i == 0){
                                            $book_output .= "<a class='uk-button-book-a-visit-hour' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($book_afternoon_from_hour).":".number_add_leading_0($book_afternoon_from_min)."'>".number_add_leading_0($book_afternoon_from_hour).":".number_add_leading_0($book_afternoon_from_min)."</a>";
                                        }else{
                                            $book_output .= "<a class='uk-button-book-a-visit-hour' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($book_afternoon_from_hour).":30'>".number_add_leading_0($book_afternoon_from_hour).":30</a>";
                                        }
                                        $book_output .= "<a class='uk-button-book-a-visit-hour' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($book_afternoon_from_hour).":00'>".number_add_leading_0($book_afternoon_from_hour).":00</a>";
                                    }
                                }
                            }else{

                                if($book_afternoon_from_min == $book_afternoon_to_min){
                                    $book_output .= "<a class='uk-button-book-a-visit-hour uk-button-book-a-visit-hour-closed' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($book_afternoon_from_hour).":00'>".number_add_leading_0($book_afternoon_from_hour).":".number_add_leading_0($book_afternoon_from_min)."</a>";
                                }else{
                                    if($book_afternoon_from_min == 0){
                                        $book_output .= "<a class='uk-button-book-a-visit-hour' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($book_afternoon_from_hour).":00'>".number_add_leading_0($book_afternoon_from_hour).":00</a>";
                                        $book_output .= "<a class='uk-button-book-a-visit-hour uk-button-book-a-visit-hour-closed' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($book_afternoon_from_hour).":".number_add_leading_0($book_afternoon_from_min)."'>".number_add_leading_0($book_afternoon_from_hour).":".number_add_leading_0($book_afternoon_from_min)."</a>";
                                    }else{
                                        $book_output .= "<a class='uk-button-book-a-visit-hour' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($book_afternoon_from_hour).":".number_add_leading_0($book_afternoon_from_min)."'>".number_add_leading_0($book_afternoon_from_hour).":".number_add_leading_0($book_afternoon_from_min)."</a>";
                                        $book_output .= "<a class='uk-button-book-a-visit-hour uk-button-book-a-visit-hour-closed' id='a-book-a-visit' data-book-day='".$book_today_day."' data-book-hour='".number_add_leading_0($book_afternoon_from_hour).":30'>".number_add_leading_0($book_afternoon_from_hour).":30</a>";
                                    }
                                }
                            }
                        }
                        
                    }

                    $book_output .= "</div>";
                }

                                                                $book_output .= "</li>";
```

Link to comment
Share on other sites

here’s how you should do this -

pre-process/expand the booked/busy array of data so that it has entries for each 30 minute interval of the appointment, using the date-time value Y-m-d H:i:s as the array index. for the example data, you would end up with three entries - 2019-11-25 13:00:00, 2019-11-25 13:30:00, and 2019-11-25 14:00:00, all with the same title as the array value (if the data for each appointment is actually more than this, store it as a sub-array under each index.)

get the day of week number from the current date you are displaying, then get the morning/afternoon start and end times for that day, loop over the 30 minute intervals, from the start time to the end time, produce the date-time value for each interval - Y-m-d H:i:s, then test (isset() will work) if there’s an entry in the pre-processed booked/busy data array for the current interval.

Link to comment
Share on other sites

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"));
	}
}

 

  • Thanks 1
Link to comment
Share on other sites

if a business rule of the application is that that booking/busy times start/end on 30 minute intervals, you would need to provide a method of picking values that follow that rule and you would need to validate submitted data to insure that it meets that rule.

if you have existing data that doesn't follow that rule, you can normalize the values, either where they are stored or when pre-processing the array of data, to round start times down to the previous interval time and round end times up to the next interval time.

Link to comment
Share on other sites

Or you could take the view that the product is a timeslot. If they want part of one and part of the next then they buy both.

Check which slots contain or over lap the required times as this example does (see line 40) ...

<?php

$business_hours = [
                     1 => [ 'am_from' => '10:00',  'am_to' => '12:00',  'pm_from' => '13:00',  'pm_to' => '19:00' ],
                     2 => [ 'am_from' => '08:00',  'am_to' => '12:00',  'pm_from' => '13:00',  'pm_to' => '17:00' ],
                     3 => [ 'am_from' => '12:00', 'am_to' => '12:00', 'pm_from' => '13:00',  'pm_to' => '19:00' ],
                     4 => [ 'am_from' => '08:00',  'am_to' => '12:00',  'pm_from' => '13:00',  'pm_to' => '17:00' ],
                     5 => [ 'am_from' => '08:00',  'am_to' => '12:00',  'pm_from' => '13:00', 'pm_to' => '13:00' ],
                  ];
//------------------------------------------------------------
// from above array, generate daily 30 minute slots
// for the next 15 days
//
$slots = [];
$dt1 = new DateTime();
$intwd = DateInterval::createFromDateString('next weekday');
$dp_days = new DatePeriod($dt1, $intwd, 14);
foreach ($dp_days as $d) {
    $dno = $d->format('w');
    $times = $business_hours[$dno];
    $slots[$d->format('Y-m-d')] = generateSlots($times);
}

//------------------------------------------------------------
//  place these bookings in their slots
//
$bookings = [  ['title' => 'Test 1', 'start' => '2019-11-26 10:15', 'end' => '2019-11-26 11:30'],
               ['title' => 'Test 2', 'start' => '2019-11-29 11:00', 'end' => '2019-11-29 12:00'],
               ['title' => 'Test 3', 'start' => '2019-12-02 15:30', 'end' => '2019-12-02 16:30']
            ];

foreach ($bookings as $b) {
    $sd = new DateTime($b['start']);
    $ed = new DateTime($b['end']);
    $date = $sd->format('Y-m-d');
    $st = $sd->format('H:i');
    $et = $ed->format('H:i');
    foreach ($slots[$date] as &$s) {
        if ($s['t'] != '') continue;                                   // not available
        if ($et > $s['s'] && $st < $s['e']) {                          // if booking is in or overlaps slot        ( LINE 40 )
            $s['t'] = $b['title'];                                     //          place title into slot
        }
    }
}

//------------------------------------------------------------
//  output the slots to the page
//
$tdata = '';
foreach ($slots as $d => $ddata) {
    if ((new DateTime($d))->format('w')==1) {
        $tdata .= "<tr><td colspan='12'> </td></tr>\n";
    }
    $tdata .= "<tr><td>" . date('D d M', strtotime($d)) . '</td>';
    foreach ($ddata as $slt) {
        switch ($slt['t']) {
            case 'closed': 
                $cls = 'class="closed"';
                $txt = ''; 
                break;
            case '': 
                $cls = ''; 
                $txt = ''; 
                break;
            default: 
                $cls = 'class="booked"';
                $txt = $slt['t']; 
        }
        $tdata .= "<td $cls>$txt</td>";
    }
    $tdata .= "</tr>\n";
}

/**
* generate daily timeslots
* 
* @param array $times      - array of business hours for the day
*/
function generateSlots($times)
{
    $stime = '';
    $dt1 = new DateTime('08:00:00');
    $dt2 = new DateTime('19:00:00');
    $di = new DateInterval('PT1H');
    $dp = new DatePeriod($dt1, $di, $dt2);
    $slots = [];
    foreach ($dp as $t) {
        $k = $t->format('G');
        $h1 = $t->format('H:i');
        $h2 = $t->modify('+1 hour')->format('H:i');
        $text =  ($h1 >= $times['am_from']) && ($h1 < $times['am_to']) || ($h1 >= $times['pm_from']) && ($h1 < $times['pm_to']) ? '' : 'closed' ;
        $slots[$k] = [ 's'=>$h1, 'e'=>$h2, 't'=>$text];
    }
    return $slots;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example Bookings</title>
<style type="text/css">
    body, table    { font-family: calibri, sans-serif; font-size: 11pt; }
    table          { border-collapse: collapse; width: 90%; margin: 0 auto; }
    th             { padding: 4px 2px; background-color: #006EFC; color: #FFF; border-color: #FFF;}
    td             { background-color: #FFF; padding: 2px; }
    td.closed      { background-color: #888; color: #FFF; }
    td.booked      { background-color: #FFC0C0; }
</style>
</head>
<body>
<header>
   <h1>Example Bookings</h1>
</header>
<table border='1'>
    <tr><th rowspan='2'>Date</th><th colspan='4'>AM</th><th></th><th colspan='6'>PM</th></tr>
    <tr><th>8 – 9</th><th>9 – 10</th><th>10 – 11</th><th>11 – 12</th>
        <th>12 – 1</th><th>1 – 2</th><th>2 – 3</th><th>3 – 4</th><th>4 – 5</th><th>5 – 6</th><th>6 – 7</th></tr>
    <?=$tdata?>
</table>
</body>
</html>

 

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.