guymclarenza Posted November 28, 2023 Share Posted November 28, 2023 I have a problem here, this script works with the exception of showing the correct days with the date Result can be seen at https://centurionphotographer.co.za/booking/ AI just gives me one different problem after another and I am not a good enough coder to solve the problem. // Number of months to display $monthsToShow = 3; // Get the current date and time $currentDate = date('Y-m-d'); // Calculate the start and end dates for the calendar $startDate = date('Y-m-d', strtotime($currentDate)); $endDate = date('Y-m-d', strtotime($currentDate . ' + ' . ($monthsToShow * 30) . ' days')); // Prepare the SQL statement to retrieve availability $availabilityQuery = $pdo->prepare(" SELECT avdate, COUNT(*) AS available_slots FROM availability WHERE avdate BETWEEN :start_date AND :end_date GROUP BY avdate "); // Prepare the SQL statement to retrieve bookings $bookingsQuery = $pdo->prepare(" SELECT bkdate, COUNT(*) AS booked_slots FROM bookings WHERE bkdate BETWEEN :start_date AND :end_date GROUP BY bkdate "); // Generate the calendar $currentMonth = ''; echo '<table>'; // Loop through each day within the specified range for ($date = $startDate; $date <= $endDate; $date = date('Y-m-d', strtotime($date . ' + 1 day'))) { $currentMonthOfDate = date('F Y', strtotime($date)); $dayOfWeek = date('w', strtotime($date)); // Check if a new month has started if ($currentMonth !== $currentMonthOfDate) { $currentMonth = $currentMonthOfDate; echo '<tr><th colspan="7">' . $currentMonth . '</th></tr>'; echo '<tr><th>Sunday</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th><th>Friday</th><th>Saturday</th></tr>'; } // Check availability for the current date $availabilityQuery->bindParam(':start_date', $date); $availabilityQuery->bindParam(':end_date', $date); $availabilityQuery->execute(); $availabilityResult = $availabilityQuery->fetchAll(PDO::FETCH_ASSOC); // Check bookings for the current date $bookingsQuery->bindParam(':start_date', $date); $bookingsQuery->bindParam(':end_date', $date); $bookingsQuery->execute(); $bookingsResult = $bookingsQuery->fetchAll(PDO::FETCH_ASSOC); // Determine the class to apply based on availability if ($availabilityResult && $availabilityResult[0]['available_slots'] > 0) { $class = 'available'; $link = '<a href="timeslots.php?date=' . $availabilityResult[0]['avdate'] . '">Check available timeslots</a>'; } elseif ($bookingsResult && $bookingsResult[0]['booked_slots'] > 0) { $class = 'fully-booked'; $link = 'Fully Booked'; } else { $class = 'unavailable'; $link = ''; } // Output the date with the appropriate class and link echo '<td class="' . $class . '">' . $date . '<br>' . $link . '</td>'; // Start a new row after Saturday if ($dayOfWeek == 6) { echo '</tr><tr>'; } } echo '</table>'; Quote Link to comment https://forums.phpfreaks.com/topic/317487-weird-problem-with-dates-on-calendar/ Share on other sites More sharing options...
gizmola Posted November 28, 2023 Share Posted November 28, 2023 I don't see the issue. Can you clarify what you mean? When I look at the calendar on your site, as an example, Dec. 3, 2023 is a Sunday, which is correct. Quote Link to comment https://forums.phpfreaks.com/topic/317487-weird-problem-with-dates-on-calendar/#findComment-1613180 Share on other sites More sharing options...
maxxd Posted November 28, 2023 Share Posted November 28, 2023 It's hard to read given the formatting (I assume that's a problem with the forum otherwise most of your code is commented out), but it looks like you're using a date range for the select queries but not looping through the results of the queries. You're printing the information out inside the loop, but only checking the [0] index in both results. Quote Link to comment https://forums.phpfreaks.com/topic/317487-weird-problem-with-dates-on-calendar/#findComment-1613187 Share on other sites More sharing options...
guymclarenza Posted November 29, 2023 Author Share Posted November 29, 2023 Fixed Quote Link to comment https://forums.phpfreaks.com/topic/317487-weird-problem-with-dates-on-calendar/#findComment-1613217 Share on other sites More sharing options...
maxxd Posted November 29, 2023 Share Posted November 29, 2023 Any chance you'd explain how it got fixed in case someone comes across this thread and it may help them? Quote Link to comment https://forums.phpfreaks.com/topic/317487-weird-problem-with-dates-on-calendar/#findComment-1613220 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.