So What I have right now is a calendar that puts the booked dates in the calendar. This is almost perfect however it only put the start and end date in the calendar. for example:
you order a hotel reservation in hotel room 101 for 4 days. The day you check in is (d-m-y): 13-6-2022
The day you will have to check out is 17-6-2022. That means that room 101 can't be available for those days and i want all those days to be red in the calendar. however this is not what is happening. it only makes the check in and the check out date red. not those days in between. I want those red as well. how can I tell my code to do this? I hope I explained it right.
the start_datum and the eind_datum is put into a db. this calendar is just simply there to show which dates have been picked and which are available.
code
start_datum = check in date
eind_datum = check out date
<?php
function build_calendar($month, $year){
include "config.php";
$sql2 = "SELECT * FROM reserveringen";
$stmt2 = $conn->prepare($sql2);
$stmt2->execute();
$result2 = $stmt2->get_result();
$bookings = array();
while ($row = $result2->fetch_assoc()) {
$start_datum = $row['start_datum'];
$eind_datum = $row['eind_datum'];
$sql = "SELECT * FROM reserveringen WHERE start_datum = ? AND eind_datum = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('ss', $start_datum, $eind_datum);
if($stmt->execute()){
$result = $stmt->get_result();
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$bookings[] = $row['start_datum'];
$bookings[] = $row['eind_datum'];
}
$stmt->close();
}
}
}
$daysOfWeek = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
$firstDayOfMonth = mktime(0,0,0, $month, 1, $year);
$numberDays = date('t', $firstDayOfMonth);
$dateComponents = getdate($firstDayOfMonth);
$monthName = $dateComponents['month'];
$dayOfWeek = $dateComponents['wday'];
$dateToday = date("Y-m-d");
$prev_month = date('m', mktime(0,0,0, $month-1, 1, $year));
$prev_year = date('Y', mktime(0,0,0, $month-1, 1, $year));
$next_month = date('m', mktime(0,0,0, $month+1, 1, $year));
$next_year = date('Y', mktime(0,0,0, $month+1, 1, $year));
$calendar = "<center><h2>$monthName $year</h2>";
$calendar.= "<a class='btn btn-primary btn-xs' href='?month=".$prev_month."&year=".$prev_year."'>Prev Month </a> ";
$calendar.= "<a class='btn btn-primary btn-xs' href='?month=".date('m')."&year=".date('Y')."'>Current Month </a> ";
$calendar.= "<a class='btn btn-primary btn-xs'href='?month=".$next_month."&year=".$next_year."'>Next Month </a></center>";
$calendar.= "<br><table class='table table-bordered'>";
$calendar.= "<tr>";
foreach($daysOfWeek as $day){
$calendar.= "<th class='header'>$day</th>";
}
$calendar.= "</tr><tr>";
$currentDay = 1;
if($dayOfWeek > 0){
for($k = 0; $k < $dayOfWeek; $k++){
$calendar.= "<td class='empty'></td>";
}
}
$month = str_pad($month, 2, "0", STR_PAD_LEFT);
while($currentDay <= $numberDays){
if($dayOfWeek == 7){
$dayOfWeek = 0;
$calendar.="</tr><tr>";
}
$currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);
$date = "$year-$month-$currentDayRel";
$dayName = strtolower(date('I', strtotime($date)));
$today = $date==date('Y-m-d')?'today':'';
if(in_array($date, $bookings)){
for ($date = $start_datum; $date <= $eind_datum; $date++) {
$calendar.="<td class='$today'><h4>$currentDay</h4><a class='btn btn-danger btn-xs'>booked</a></td>";
}
}else{
$calendar.="<td class='$today'><h4>$currentDay</h4><a class='btn btn-success btn-xs'>Beschrikbaar</a></td>";
}
$currentDay++;
$dayOfWeek++;
}
if($dayOfWeek<7){
$remainingDays = 7 - $dayOfWeek;
for($i = 0; $i<$remainingDays; $i++){
$calendar.= "<td class='empty'></td>";
}
}
$calendar.= "</tr></table>";
return $calendar;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<title>Kalender</title>
<style>
@media only screen and (max-width: 760px),
(min-device-width: 802px) and (max-device-width: 1020px){
table,
thead,
tbody,
th,
td,
tr{
display: block;
}
.empty{
display: none;
}
th{
position: absolute;
top: -9999px;
left: -9999px;
}
tr{
border: 1px solid #ccc;
}
td{
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 50%;
}
td:nth-of-type(1):before{
content: "Sunday";
}
td:nth-of-type(2):before{
content: "Monday";
}
td:nth-of-type(3):before{
content: "Tuesday";
}
td:nth-of-type(4):before{
content: "Wednesday";
}
td:nth-of-type(5):before{
content: "Thursday";
}
td:nth-of-type(6):before{
content: "Friday";
}
td:nth-of-type(7):before{
content: "Saturday";
}
}
@media only screen and (min-device-width: 320px) and (max-device-width: 480px){
body{
padding: 0;
margin: 0;
}
}
@media only screen and (min-device-width: 802px) and (max-device-width: 1020px){
body{
width: 485px;
}
}
@media (min-width: 641px){
table{
table-layout: fixed;
}
td{
width: 33%;
}
}
.row{
margin-top: 20px;
}
.today{
background: yellow;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<?php
$dateComponents = getdate();
if(isset($_GET['month']) && isset($_GET['year'])){
$month = $_GET['month'];
$year = $_GET['year'];
}else{
$month = $dateComponents['mon'];
$year = $dateComponents['year'];
}
echo build_calendar($month, $year);
?>
</div>
</div>
</div>
</body>
</html>
What I have tried:
I tried to google it but I don't know how to explain myself well. tried searching up things like "
php mysqli calendar make days in between red
" but that's not giving me the right results