PHP日历:迭代选定的月份?

时间:2011-12-09 02:57:23

标签: php calendar

看了一会儿,现在我的眼睛越过了。 :)

我有一个日历脚本I found on snipplr.com它很可爱。道具给创作者。现在,我有两件事我想自定义。

目前,日历在1月至12月期间吐出12个月。周末也是星期日(grrrr)。我试图从这个月开始,加上x个月。例如,它将显示12月,再加上5个月,所以12月,1月,2月,3月,4月,5月。

我可以在代码中告诉它使用$ i迭代来完成月份并显示相应的日期。为($ I = 1; $ I< = 11; $ I ++)

所以,我试着改变它:for($ i = $ this_month; $ i< = 11; $ i ++)
$ this_month当然是日期('m');

它确实成功显示了12月,但没有几个月。 (因为它在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 个答案:

答案 0 :(得分: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
    ...
}
相关问题