tags:

views:

144

answers:

3

I have a php calendar at http://idea-palette.com/aleventcal/calendar.php. I want the days that are not part of the current month to show up grayed out(which it does now), and I want it to appear as those are the days of the previous and next month.

As it is now, the days that appear before the first day of the month appear as negative numbers(-1,-2,-3, and so on) and the days that appear after the last day of the month just continue on, so if a month ends on the 31st, then it would read 32, 33, 34, and so on.

I'm trying to figure out a conditional statement with some sort of loop where I could see if it is greater than the total days and then do something else. The problem I see is that the table cell that is being created is being looped, so if I do just $day+1, then instead of 32, it will just read 33.

Here's my code:

for($i=0; $i< $total_rows; $i++)
{
    for($j=0; $j<7;$j++)
    {
        $day++;     

        //if the current day is less or equal to the total days in the month           
        if($day>0 && $day<=$total_days_of_current_month)
        {
            $date_form = "$current_year/$current_month/$day";

            echo '<div class="date_has_event" href="#"><td';

            //If the date is today then give the td cell the 'today' class
            if($date_form == $today)
            {
                echo ' class="today"';
            }

            //check if any event stored for the date
            if(array_key_exists($day,$events))
            {
                //adding the date_has_event class to the <td> and close it
                echo ' class="date_has_event">'.$day;

                //adding the eventTitle and eventContent wrapped inside <span> & <li> to <ul>
                echo '<div class="events"><ul>'.$events[$day].'</ul></div>';
            }
        }   
        else //if the current day is less or more than the total days in the month 
        {
            //then create a table cell with the current day of the mont
            echo '<td class="padding">' . $day . '&nbsp;</td>'; h
        }
    }
}
+1  A: 

just subtract the number of days in the current month of the day is positive:

else //if the current day is less or more than the total days in the month 
{
    if($day > 1){
     echo '<td class="padding">' . ($day - $total_days_of_current_month) . ' </td>';      // the next month
    } else {
     echo '<td class="padding">' . $day . ' </td>';       //then create a table cell with the current day of the month
    }
}
Steve Brewer
This worked well for the numbers that are above the total number of days in a month, but the numbers that come before the first day of the month need to say 31, 30, 29 and so on, but that is difficult because some months have 30 days and some months have 31 days.
zeckdude
I'm not sure what the problem is Chris. http://ca2.php.net/manual/en/function.date.php 't' will give you the number of days in the month. Just need to give it a bit of thought; all the hard stuff is already given to you by PHP.
Mark
+1  A: 

Here's part of a calendar function I recently wrote, you can hopefully get some ideas from it.

// $month is a UNIX timestamp

// resetting to 1st of the month
$date = getdate(mktime(0, 0, 0, date('n', $month), 1, date('Y', $month)));

// resetting to first day in grid
$date = getdate(strtotime("-$date[wday] days", $date[0]));

$out = array();

$lastDay = mktime(0, 0, 0, date('n', $month), date('t', $month), date('Y', $month));

while ($date[0] <= $lastDay) {

    $row = array();

    for ($x = 0; $x <= 6; $x++) {
        $attr = array('class' => 'weekday '.low(date('D', $date[0])));
        if (date('n', $month) != $date['mon']) {
            $attr['class'].= ' prevNextMonth';
        }
        if (date('Y-m-d') == date('Y-m-d', $date[0])) {
            $attr['class'].= ' today';
        }

        $row[] = array($date['mday'], $attr);

        $date = getdate(strtotime("+1 day", $date[0]));
    }

    $out[] = $row;
}

// makes table rows out of the array, considers the $attr array as well
$out = $this->Html->tableCells($out);
$out = sprintf('<table><tbody>%s</tbody></table>', $out);
deceze
A: 

You don't need Steve's conditional: instead use

echo '<td class="padding">' . date("j",mktime(12,0,0,$current_month,$day,$current_year)) . ' </td>';

mktime handles out-of-range dates my moving them into the next or previous month, which is exactly what you want.

Robert L