PHP Jahreskalender

Kalendervariante 1

<?php
// Aktuelles Datum
$currentYear = date('Y');
$currentDate = date('Y-m-d');

// Kalender erstellen
$calendar = '<table>';

// Monate durchlaufen
for ($month = 1; $month <= 12; $month++) {
    // Ersten Tag des Monats ermitteln
    $firstDayOfMonth = date('N', strtotime($currentYear . '-' . $month . '-01'));

    // Anzahl der Tage im Monat ermitteln
    $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $currentYear);

    // Tabellenkopf für Monat erstellen
    $calendar .= '<tr><th colspan="7">' . date('F', strtotime($currentYear . '-' . $month . '-01')) . '</th></tr>';
    $calendar .= '<tr><th>Mo</th><th>Di</th><th>Mi</th><th>Do</th><th>Fr</th><th>Sa</th><th>So</th></tr>';

    // Kalendertage erstellen
    $calendar .= '<tr>';

    // Leere Zellen bis zum ersten Tag des Monats
    for ($i = 1; $i < $firstDayOfMonth; $i++) {
        $calendar .= '<td></td>';
    }

    // Tage des Monats durchlaufen
    for ($day = 1; $day <= $daysInMonth; $day++) {
        $date = $currentYear . '-' . $month . '-' . $day;
        $dayOfWeek = date('N', strtotime($date));
        $cellClass = ($date == $currentDate) ? 'current-day' : '';
        $cellStyle = ($dayOfWeek == 7) ? 'color: red;' : '';

        $calendar .= '<td style="' . $cellStyle . '">';
        $calendar .= '<div class="' . $cellClass . '">' . $day . '</div>';
        $calendar .= '</td>';

        // Neue Woche beginnen
        if ($dayOfWeek == 7) {
            $calendar .= '</tr><tr>';
        }
    }

    // Leere Zellen nach dem letzten Tag des Monats
    if ($dayOfWeek != 7) {
        for ($i = $dayOfWeek + 1; $i <= 7; $i++) {
            $calendar .= '<td></td>';
        }
    }

    $calendar .= '</tr>';
}

$calendar .= '</table>';
?>

<!DOCTYPE html>
<html>
<head>
    <style>
        table {
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid black;
            padding: 5px;
            text-align: center;
        }
        .current-day {
            background-color: green;
        }
    </style>
</head>
<body>
    <h1>Jahreskalender</h1>
    <?php echo $calendar; ?>
</body>
</html>

 

 

Kalendervariante 2

<?php
$year = date("Y");
$months = array(
    "Januar", "Februar", "März", "April", "Mai", "Juni",
    "Juli", "August", "September", "Oktober", "November", "Dezember"
);

echo "<table>";
echo "<tr><th colspan=\"4\">$year</th></tr>";

for ($row = 0; $row < 3; $row++) {
    echo "<tr>";
    for ($col = 0; $col < 4; $col++) {
        $monthIndex = $row * 4 + $col;
        $month = $monthIndex + 1;
        $firstDay = date("N", strtotime("$year-$month-01"));
        $totalDays = date("t", strtotime("$year-$month-01"));
        
        echo "<td>";
        echo "<table>";
        echo "<tr><th colspan=\"7\">".$months[$monthIndex]."</th></tr>";
        echo "<tr><th>Mo</th><th>Di</th><th>Mi</th><th>Do</th><th>Fr</th><th>Sa</th><th>So</th></tr>";
        
        $day = 1;
        $dayOfWeek = 1;
        echo "<tr>";
        while ($day <= $totalDays) {
            if ($dayOfWeek == 1) {
                echo "</tr><tr>";
            }
            
            echo "<td>";
            if ($day == date("d") && $month == date("n")) {
                echo "<span style=\"background-color: #ff0000; color: #ffffff;\">$day</span>";
            } elseif ($dayOfWeek == 6) {
                echo "<span style=\"color: #0000cc;\">$day</span>";
            } elseif ($dayOfWeek == 7) {
                echo "<span style=\"color: #cc0000;\">$day</span>";
            } else {
                echo $day;
            }
            echo "</td>";
            
            $day++;
            $dayOfWeek++;
            if ($dayOfWeek > 7) {
                $dayOfWeek = 1;
            }
        }
        
        echo "</tr>";
        echo "</table>";
        echo "</td>";
    }
    echo "</tr>";
}

echo "</table>";
?>

 

Hinterlasse jetzt einen Kommentar

Kommentar hinterlassen

E-Mail Adresse wird nicht veröffentlicht.


*


Diese Seite ist durch reCAPTCHA und Google geschütztDatenschutz-Bestimmungen UndNutzungsbedingungen anwenden.