在MediaWiki中编辑当前月份的PHP代码

时间:2012-10-16 20:42:56

标签: php calendar mediawiki

我正在制作一个维基,我正在建立一个社区日历。我正在使用SimpleCalendar扩展程序,我想显示当前月份,上个月和下个月。当前月份功能正常,但我无法弄清楚如何显示上个月和下个月

维基

{{#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|}";

}

你必须原谅我的无知。我是PHP的新手。

2 个答案:

答案 0 :(得分:0)

您的代码很难阅读恕我直言。你看起来似乎在乱搞“29”天......你实际上想要做的只是构造一个时间(使用mktime),月份参数为$ month-1。这个精彩的小功能将为您处理所有“月= 0因此月= 12和年=年-1”的东西。如果您提供的日期是2012年5月31日被发送到fn作为“2012年4月31日”并且将返回“2012年4月30日”,它将同样处理:)我认为如果您的代码可能很容易小于5的大小优先这样做。如果您需要任何进一步的帮助,请回复,我会看到我能做什么。

答案 1 :(得分:0)

我明白了!我只需要将ParserFunction功能添加到我的LocalSettings.php然后使用

{{#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 }}