Loader

false));
date_default_timezone_set(‘Europe/London’); // Set to the desired time zone
// Check if the data is valid
if (isset($data[‘data’]) && is_array($data[‘data’])) {
$regular_items = [];
$items_with_t = [];

// Loop through each item in the results array
foreach ($data[‘data’] as $item) {
// Check if the custom field value ends with ‘[t]’
if (isset($item[‘custom_fields’][25][‘value_string’])) {
$value_string = $item[‘custom_fields’][25][‘value_string’];
if (substr($value_string, -3) === ‘[T]’) {
$items_with_t[] = $item;
} else {
$regular_items[] = $item;
}
} else {
// If the custom field does not exist or does not have a value, treat as a regular item
$regular_items[] = $item;
}
}

// Merge the two arrays, with items ending with ‘[t]’ at the end
$sorted_data = array_merge($regular_items, $items_with_t);
} else {
$sorted_data = []; // No data available or invalid data
}
// Initialize arrays
$hours = range(0, 23);
$availability = array_fill(0, 24, 0);
// Debugging: Print out the raw data to ensure it’s being read correctly
//echo ‘

' . print_r($data, true) . '

‘;
foreach ($sorted_data as $item) {
// Correctly instantiate DateTime objects with timezone
$end = new DateTime($item[‘duty_status’][‘enddate’], new DateTimeZone(‘Europe/London’));
$start = new DateTime($item[‘duty_status’][‘startdate’], new DateTimeZone(‘Europe/London’));

// Format the DateTime objects
//echo “Start DateTime: ” . $start->format(‘Y-m-d H:i:s’) . “\n”;
// echo “End DateTime: ” . $end->format(‘Y-m-d H:i:s’) . “\n”;
// Extract hours from DateTime objects
$startHour = (int)$start->format(‘G’); // ‘G’ for 24-hour format without leading zeros
$endHour = (int)$end->format(‘G’);

// Debugging: Print start and end hours
// echo “Start Hour: $startHour, End Hour: $endHour\n”;
// Handle case where end hour is less than start hour (e.g., availability crosses midnight)
if ($endHour < $startHour) { // Increment for hours from start to end of the day for ($i = $startHour; $i < 24; $i++) { $availability[$i]++; } // Increment for hours from start of the day to end for ($i = 0; $i <= $endHour; $i++) { $availability[$i]++; } } else { // Normal case where start and end are within the same day for ($i >= $startHour; $i <= $endHour; $i++) { $availability[$i]++; } } } // Encode the processed data as JSON $hoursJson = json_encode($hours); $availabilityJson = json_encode($availability); ?>






Availability Chart