query($sql); $currentDate = new DateTime(); if ($result->num_rows > 0) { // Array to hold the results $count = 0; $rows = array(); // Fetch data from each row and add it to the array while($row = $result->fetch_assoc()) { // Create DateTime object for a given date $futureDate = new DateTime($row['date']); // Get the difference $interval = $futureDate->diff($currentDate); if($interval->days >1){ $withS = 's'; }else{ $withS = ''; } $intervalDates = $interval->days ." day".$withS; if($intervalDates > 5){ $myCSS = 'daysRed'; }else{ $myCSS = 'daysGreen'; } $count =$count + 1; $rows[] = [$row,"days"=>"$intervalDates","myCSS"=>"$myCSS",]; } // Output the result in JSON format header('Content-Type: application/json'); echo json_encode([$rows,"Count" => "$count"]); } } // Close connection $conn->close(); ?>