1

I'm working on a wiki and I'm building a community calendar. I'm using the SimpleCalendar extension and I'd like to display the current month, the previous, and next month. The current month feature works fine, but I'm having trouble figuring out how to display the previous and following month

Wiki

{{#calendar: month=$prevMonth | dayformat=%A | format=%A %B %d %Y }}
{{#calendar: month={{CURRENTMONTH}} | dayformat=%A | format=%A %B %d %Y }}
{{#calendar: month=$nextMonth | dayformat=%A | format=%A %B %d %Y }}

PHP

function wfRenderMonth($m,$y,$prefix = '',$query = '',$format = '%e %B %Y',$dayformat = false) {
$thisDay   = date('d');
$thisMonth = date('n');
$thisYear  = date('Y');
if (!$d = date('w',$ts = mktime(0,0,0,$m,1,$y))) $d = 7;
$month = wfMsg(strtolower(strftime('%B',$ts)));
//I've been editing these two lines to try to create a variable for the previous and following months
$prevMonth = "strftime(%m - 1)"; 
$nextMonth = "strftime(%m + 1)";
$days = array();
foreach (array('M','T','W','T','F','S','S') as $i => $day)
    $days[] = $dayformat ? wfMsg(strftime($dayformat,mktime(0,0,0,2,$i,2000))) : $day;
$table = "\n{| class=\"month\"\n|- class=\"heading\"\n|colspan=\"7\"|$month\n|- class=\"dow\"\n";
$table .= '|'.join('||',$days)."\n";
if ($d > 1) $table .= "|-".str_repeat("\n| \n",$d-1);
for ($i = $day = $d; $day < 32; $i++) {
    $day = $i - $d + 1;
    if ($day < 29 or checkdate($m,$day,$y)) {
        if ($i%7 == 1) $table .= "\n|-\n";
        $t = ($day == $thisDay and $m == $thisMonth and $y == $thisYear) ? ' class="today"' : '';
        $ttext = $prefix.trim(strftime($format,mktime(0,0,0,$m,$day,$y)));
        $title = Title::newFromText($ttext);
        if (is_object($title)) {
            $class = $title->exists() ? 'day-active' : 'day-empty';
            $url = $title->getFullURL($title->exists() ? '' : $query);
        } else $url = $ttext;
        $table .= "|$t|[$url <span class='$class'>$day</span>]\n";
    }
}
return "$table\n|}";

}

You will have to forgive my noobness. I'm very new to PHP.

4

2 回答 2

0

Your code is very difficult to read IMHO. You appear to be messing around with "29" days etc... what you in fact want to do is simply construct a time (using mktime) with $month-1 for the month parameter. This wonderful little function will handle all the "month=0 therefore month=12 and year=year-1" stuff for you. It will equally handle if the date you give is 31 May 2012 being sent to the fn as "31 April 2012" and will return "30 April 2012" :) I think your code could easily be less than a 5th of the size if you do this in preference. If you want any further help, please do post back, and I'll see what I can do.

于 2012-10-16T20:54:45.257 回答
0

I figured it out! I just needed to add ParserFunction capabilities to my LocalSettings.php and then use

{{#calendar: month={{#expr:{{formatnum:{{CURRENTMONTH}}|R}}-1}} | dayformat=%A |format=%A %B %d %Y }}
{{#calendar: month={{CURRENTMONTH}} | dayformat=%A | format=%A %B %d %Y }}
{{#calendar: month={{#expr:{{formatnum:{{CURRENTMONTH}}|R}}+1}} | dayformat=%A | format=%A %B %d %Y }}
于 2012-10-17T16:06:06.340 回答