Creating Calendar in PHP



by Administrator author list

Step 3.To display a calendar we need a table with 7 columns for the days of the week. The number of lines depending on the number of days and the first day of the month. However we need a header line with month and year information, a subheader line with the name of the days.

<?php
    
// Create a table with the necessary header informations
    
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>';
?>

Step 4.Ok, now we have the header of the table. Let's try to fill the first row. It is not so easy as you can not just write 1 in the first cell, 2 in the second and so on. It only works if the first day of the month was Monday, but what if not? To decide this we need the wday item from the firstDay array. With this information we can fill the cells with a space if needed. The code to do this is the follows:

<?php
    
echo '<tr>';
    for(
$i=1;$i<$firstDay['wday'];$i++){
        echo 
'<td>&nbsp;</td>';
    }
    
$actday 0;
    for(
$i=$firstDay['wday'];$i<=7;$i++){
        
$actday++;
        echo 
"<td>$actday</td>";
    }
    echo 
'</tr>';
?>

Step 5.As next step we need to fill to following lines. It is a bit easier, we only need to know how many full week we have and fill some table rows as follows:

<?php
    $fullWeeks 
floor(($lastDay['mday']-$actday)/7);
    
    for (
$i=0;$i<$fullWeeks;$i++){
        echo 
'<tr>';
        for (
$j=0;$j<7;$j++){
            
$actday++;
            echo 
"<td>$actday</td>";
        }
        echo 
'</tr>';
    }
?>



article index
page 1 : untitled page
page 2 - current : Part 2
page 3 : Part 3


Tags:

Php Toys - 2006 - Php resources, scripts and tutorials - Privacy Policy
Insurance Index - Tutorial collection - Forex trading, brokers, reviews - Mortgage payment calculator
{THEMEDISCLAIMER}
Render time: 0.1510 sec, 0.0460 of that for queries. DB queries: 25.