Jump to content

weird problem with dates on calendar


guymclarenza

Recommended Posts

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>';

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.