2011-12-09 94 views
-1

一直在看这一点,现在我的眼睛越过。 :)PHP日历:遍历选定的月份?

我有一个日历脚本I found on snipplr.com这很漂亮。道具给创作者。现在,我想要定制两件事。

现在,日历吐出12个月,1月至12月。并且这个星期在星期天结束(grrrr)。我试图从本月开始,再加上x个月。例如,它会显示十二月,再加上五个月,十二月,一月,二月,三月,四月,五月。

我可以告诉代码它使用$ i迭代来获取月份并显示适当的日期。为($ i = 1; $ I < = 11; $ I ++)

SO,我试图changint它这样:for($ i = $ THIS_MONTH; $ I < = 11; $ I ++)
$ THIS_MONTH当然是日期('m');

它确实成功显示十二月,但没有几个月后。 (因为它停在11)。但是,如果我将11添加到$ this_month + 5的另一个变量,那么该脚本不知道13,14和15个月是什么。

对此有帮助吗?这是我迄今为止的整个剧本。

function days_in_month($month, $year) { 
    if($month!=2) { 
     if($month==9||$month==4||$month==6||$month==11) 
      return 30; 
     else 
      return 31; 
    } 
    else 
     return $year%4==""&&$year%100!="" ? 29 : 28; 
} 

global $months; 
$months = array(0 => 'January', 1 => 'February', 2 => 'March', 3 => 'April', 4 => 'May', 5 => 'June', 6 => 'July', 7 => 'August', 8 => 'September', 9 => 'October', 10 => 'November', 11 => 'December'); 
$days = array(0 => 'Monday', 1 => 'Tuesday', 2 => 'Wednesday', 3 => 'Thursday', 4 => 'Friday', 5 => 'Saturday', 6 => 'Sunday'); 

function render_calendar($this_year = null) { 
    if($this_year==null) 
     $this_month = date('m')-1; 
     $first = strtotime(date('m')); 
     $last = strtotime("+6 months", $this_month); 
     $this_year = date('Y'); 

    $day_of_the_month = date('N', strtotime('1 January '.$this_year)); 
    for($i=$this_month;$i<=12;$i++) { 

//  echo $i; 
//  if ($i==12) { 
//   $i = 0; 
//  } 
     echo $i; 
     echo "<table> 
      <caption>".$GLOBALS['months'][$i]."</caption> 
      <thead> 
       <tr> 
        <th>Sun</th> 
        <th>Mon</th> 
        <th>Tue</th> 
        <th>Wed</th> 
        <th>Thu</th> 
        <th>Fri</th> 
        <th>Sat</th> 

       </tr> 
      </thead> 
      <tbody> 
       <tr>"; 
     for($n=1;$n<$day_of_the_month;$n++) 
      echo "<td></td>\n"; 
     $days = days_in_month($i+1, $this_year); 
     $day = 0;  
     while($day<$days) { 
      if($day_of_the_month==8) { 
       echo ($day == 0 ? "" : "</tr>\n") . "<tr>\n"; 
       $day_of_the_month = 1; 
      } 
      echo "<td style=\"border: 1px solid red;\">" . ($day+1) . "</td>\n"; 
      $day_of_the_month++; 
      $day++; 
     } 
     echo "</tr> 
      </tbody> 
     </table>"; 
    } 
} 

回答

1

这个怎么样为你循环:

for($i=$this_month;$i<=$this_month+5;$i++) { 
    // create a variable to hold the proper month index 
    $currentMonth = $i; 
    if ($i>11) { 
     $currentMonth -= 12; 
    } 
    // now replace all references to the $i index with $currentMonth 
    ... 
} 
+0

美丽的先生!正是我在找什么。因此,虽然$ i变量可能会达到15,16,17,但您可以通过减去12来设置当前的BACK返回值......辉煌......现在将这个日历变为Sun日历而不是Mon日至Sun!再次感谢! – NarfFlarf