Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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";
    //Hier moet de mysqli select query
    $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>";
    // hier boven is een systeem dat werkt voor volgende maand enzovoort misschien kan ik dit gebruiken in de andere kalender
    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";
        //niet zeker of dit een l een i of een I is 
        $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>";
            }
           // $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";
            }
        }
            /*voor smarthphones */
            @media only screen and (min-device-width: 320px) and (max-device-width: 480px){
                body{
                    padding: 0;
                    margin: 0;
                }
            }
            /*voor Ipads */
            @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
Posted
Updated 13-Jun-22 3:28am
v3
Comments
Peter_in_2780 13-Jun-22 8:06am    
You need to set up a loop, from your start day to your end day, coloring each one as you go.
minoesje minoes 13-Jun-22 8:35am    
so I did
for ($date = $start_datum; $date <= $eind_datum; $date++) {             $calendar.="<h4>$currentDay</h4><a class="btn btn-danger btn-xs">booked</a>";            }


^

but this just adds 4 extra start dates and 3 extra end days
so calendar will look like:
13 booked, 13 booked, 13 booked, 14 free, 15 booked, 15 booked, 15 booked
Richard MacCutchan 13-Jun-22 8:51am    
You are adding the same date ($currentDay) for each entry. You should be adding the $date variable, which is the one that varies.
minoesje minoes 13-Jun-22 9:04am    
maybe I understand it wrong, But if I change $currentDay to $date I will get almost the same result. the only difference is that it's 2022-06-13 now instead of 13.
so for the record what I changed:
PHPCopy Code
for ($date = $start_datum; $date <= $eind_datum; $date++) {             $calendar.="<td class='$today'><h4>$date</h4><a class='btn btn-danger btn-xs'>booked</a></td>";            }


It might be important information but $date contains 2 dates which is $start_datum and $eind_datum.

1 solution

I have tried a few things and can only get it to work by using timestamp values as follows:
PHP
$start_datum = mktime(0, 0, 0, 6, 10, 2022);
$eind_datum = mktime(0, 0, 0, 6, 14, 2022);
$seconds = 24 * 60 * 60;
for ($date = $start_datum; $date <= $eind_datum; $date += $seconds) {
    $next = date("F j, Y", $date);
     echo "<td class='today'><h4>$next</h4><a class='btn btn-danger btn-xs'>booked</a></td>\n";
}

You will need to find a way to convert your $start_datum and $eind_datum values to timestamp tyoes.
 
Share this answer
 
Comments
minoesje minoes 14-Jun-22 3:02am    
been looking around and I have this right now
$timestampStart_datum = strtotime($start_datum);
        $timestampEind_datum = strtotime($eind_datum);
        $seconds = 24 * 60 * 60;
            for ($date = $timestampStart_datum; $date <= $timestampEind_datum; $date += $seconds) {
                $next = date("F j, Y", $date);
             $calendar.="<td class='$today'><h4>$next</h4><a class='btn btn-danger btn-xs'>booked</a></td>";
            }
           // $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>";
        }

Still gives me the same problem. maybe you see what I'm doing wrong here?
Richard MacCutchan 14-Jun-22 3:49am    
I just fed two sample dates into that code and it works correctly:
start_datum: 2022/06/12
eind_datum: 2022/06/14
<td class='TODAY'><h4>June 12, 2022</h4><a class='btn btn-danger btn-xs'>booked</a></td>
<td class='TODAY'><h4>June 13, 2022</h4><a class='btn btn-danger btn-xs'>booked</a></td>
<td class='TODAY'><h4>June 14, 2022</h4><a class='btn btn-danger btn-xs'>booked</a></td>

You need to do some tests to check that your date conversions are working.
minoesje minoes 14-Jun-22 3:53am    
yeah I found out something weird $start_datum contains the $start_datum twice same thing goes for $eind_datum. Don't know why this is the case but I'm trying to look into it. ( this happens when I say)
$timestampStart_datum = strtotime($start_datum);
        $timestampEind_datum = strtotime($eind_datum);
        $seconds = 24 * 60 * 60;
        echo "<br>";
        echo $start_datum; // gives this date twice

for ($date = $timestampStart_datum; $date <= $timestampEind_datum; $date += $seconds) {
                $next = date("F j, Y", $date);
             $calendar.="<td class='$today'><h4>$next</h4><a class='btn btn-danger btn-xs'>booked</a></td>";
            }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900