2012-10-16 74 views
1

我正在創建一個wiki,並且正在構建一個社區日曆。我使用的是SimpleCalendar擴展,我想顯示當前月份,上個月和下個月。當月功能工作正常,但我無法找出如何顯示之前和之後的一個月編輯當前月份MediaWiki中的PHP代碼

維基

{{#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很陌生。

回答

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 }} 
0

你的代碼很難閱讀恕我直言。你似乎正在搞「29」天等等......你實際上想要做的只是構建一個時間(使用mktime)月份參數$ month-1。這個美妙的小函數將爲你處理所有的「月= 0,因此月= 12和年= 1年」的東西。如果您給出的日期是2012年5月31日發送給fn爲「2012年4月31日」,並且將返回「2012年4月30日」,那麼它將同樣處理。如果您認爲您的代碼很容易小於五分之一優先執行此操作。如果您需要更多幫助,請回復,我會看看我能做些什麼。