Home >> Snippet List >> Snippet

Name
Basic PHP Calendar
Description
Easy to modify PHP Calendar that displays the current month
PHP Snippet
<?PHP
 
FUNCTION showCalendar(){
 
    // get the first and last day of the month, and the actual day
    $today    = GETDATE();
    $firstDay = GETDATE(MKTIME(0,0,0,$today['mon'],1,$today['year']));
    $lastDay  = GETDATE(MKTIME(0,0,0,$today['mon']+1,0,$today['year']));
 
    // Create a table with the necessary header information
    ECHO '<table>';
    ECHO '  <tr><th colspan="7">'.$today['month']." - ".$today['year']."</th></tr>";
    ECHO '<tr class="days">';
    ECHO '  <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
    ECHO '  <td>Fr</td><td>Sa</td><td>Su</td></tr>';
 
     // Display the first calendar row with 
        //correct positioning
    ECHO '<tr>';
    FOR($i=1;$i<$firstDay['wday'];$i++){
        ECHO '<td> </td>';
    }
    $actday = 0;
    FOR($i=$firstDay['wday'];$i<=7;$i++){
        $actday++;
        ECHO "<td>$actday</td>";
    }
    ECHO '</tr>';
 
    // Get how many complete weeks are in the actual month
    $fullWeeks = FLOOR(($lastDay['mday']-$actday)/7);
 
    FOR ($i=0;$i<$fullWeeks;$i++){
        ECHO '<tr>';
        FOR ($j=0;$j;$j++){
            $actday++;
               ECHO "<td>$actday</td>";
        }
        ECHO '</tr>';
    }
 
    / /Now display the rest of the month
    IF ($actday < $lastDay['mday']){
        ECHO '<tr>';
 
        FOR ($i=0;$i;$i++){
            $actday++;
            IF ($actday <= $lastDay['mday']){
                ECHO "<td>$actday</td>";
            }
            ELSE {
                ECHO '<td> </td>';
            }
        }
 
 
        ECHO '</tr>';
    }
 
    ECHO '</table>';
}
 
showCalendar();
 
?>