Search code examples
phpcalendargetdate

Echo all months with days calendar


I am using the code below to echo the current month. How can i enhance it so that is shows all the months with the names and days and dates..

Code:

 <?php
$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']));


?>

<?php

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>';
?> 

<?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>';
?> 

<?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>';
    }
    ?> 

    <?php
    if ($actday < $lastDay['mday']){
    echo '<tr>';

    for ($i=0; $i<7;$i++){
        $actday++;
        if ($actday <= $lastDay['mday']){
            echo "<td>$actday</td>";
        }
        else {
            echo '<td>&nbsp;</td>';
        }
    }

    echo '</tr>';
}
?> 

Solution

  • Try this:

    function getDates($year)
    {
        $dates = array();
    
        date("L", mktime(0,0,0, 7,7, $year)) ? $days = 366 : $days = 365;
        for($i = 1; $i <= $days; $i++){
            $month = date('m', mktime(0,0,0,1,$i,$year));
            $wk = date('W', mktime(0,0,0,1,$i,$year));
            $wkDay = date('D', mktime(0,0,0,1,$i,$year));
            $day = date('d', mktime(0,0,0,1,$i,$year));
    
            $dates[$month][$wk][$wkDay] = $day;
        } 
    
        return $dates;   
    }
    

    it will return an array of months->weeks->day->weekday of the year you pass to the function. Hopefully it should be easy to traverse through the array to print everything out. Am sure there are a lot of tweaks you can make to that but its a start.

    I would also try and stay away from printing out html using echo, for example instead of;

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

    do;

    <tr>;
    <?php for($i=1;$i<$firstDay['wday'];$i++){ ?>
        <td><?php echo $var; ?></td>
    <?php } ?>
    

    It kind of makes the code more readable I think.

    EDIT: Just thought I should include an example of a use case as well, as below:

    <?php $dates = getDates(2011); 
    
    $weekdays = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'); ?>
    <?php foreach($dates as $month => $weeks) { ?>
    <table>
        <tr>
            <th><?php echo implode('</th><th>', $weekdays); ?></th>
        </tr>
        <?php foreach($weeks as $week => $days){ ?>
        <tr>
            <?php foreach($weekdays as $day){ ?>
            <td>
                <?php echo isset($days[$day]) ? $days[$day] : '&nbsp'; ?>
            </td>               
            <?php } ?>
        </tr>
        <?php } ?>
    </table>
    <?php } ?>
    

    Which gives you the output:

    enter image description here