user2737457
user2737457

Reputation: 293

Next Months PHP Calendar

I have a web page where I am putting 4 simple calendars which show the dates for the next 4 months. I have got the code for showing the current month but I'm unsure of how to edit the code to show the second, third and fourth months. Here is the code that I am using to find the current month:

<table>
<?php
$today = date("d"); // Current day
$month = date("m"); // Current month
$year = date("Y"); // Current year
$days = cal_days_in_month(CAL_GREGORIAN,$month,$year); // Days in current month

$lastmonth = date("t", mktime(0,0,0,$month-1,1,$year)); // Days in previous month

$start = date("N", mktime(0,0,0,$month,1,$year)); // Starting day of current month
$finish = date("N", mktime(0,0,0,$month,$days,$year)); // Finishing day of  current month
$laststart = $start - 1; // Days of previous month in calander

$counter = 1;
$nextMonthCounter = 1;

if($start > 5){ $rows = 6; }else {$rows = 5; }
for($i = 1; $i <= $rows; $i++){
    echo '<tr class="week">';
    for($x = 1; $x <= 7; $x++){             

        if(($counter - $start) < 0){
            $date = (($lastmonth - $laststart) + $counter);
            $class = 'class="blur"';
        }else if(($counter - $start) >= $days){
            $date = ($nextMonthCounter);
            $nextMonthCounter++;

            $class = 'class="blur"';

        }else {
            $date = ($counter - $start + 1);
            if($today == $counter - $start + 1){
                $class = 'class="today"';
            }
        }


        echo '<td '.$class.'><a class="date">'. $date . '</a></td>';

        $counter++;
        $class = '';
    }
    echo '</tr>';
}
?>
</table>

What variables do I need to change to make this work for future months?

Upvotes: 0

Views: 1922

Answers (1)

Elon Than
Elon Than

Reputation: 9765

To get next month, you have to just change $month variable, eg. like this date("m", strtotime('+1 month'));

Upvotes: 1

Related Questions