0

カレンダーを生成するための簡単な PHP スクリプトを作成しました。週の丸め (つまり、月が金曜日に始まる場合、その週の月曜日に生成されます)、ナビゲーションなどがあります。うまく機能しますが、10 月にバグがあります。毎月最終日曜日に2回抽選します。私は日曜日を新しい行の記号として使用するので、

私のカレンダーの例

10 月バグのあるカレンダーの例

コードは次のとおりです (私は専門家ではありません。本はなく、Google と PHP のマニュアルだけで、独学で PHP を学びました):

    <?php
    function getfirstday($month, $year)
    {
        $datestr = "01-$month-$year";
        $day = date('N', strtotime($datestr));
        return $day;
    }

    function getlastday($month, $year)
    {
        $datestr = cal_days_in_month(CAL_GREGORIAN, $month, $year)."-$month-$year";
        $day = date('N', strtotime($datestr));
        return $day;
    }
    //Don't care about this, I just want to have weekdays in my primary language
    function getweekday($weekDay) {
        $list = array();
        $list['1'] = "Pondělí";
        $list['2'] = "Úterý";
        $list['3'] = "Středa";
        $list['4'] = "Čtvrtek";
        $list['5'] = "Pátek";
        $list['6'] = "Sobota";
        $list['7'] = "Neděle";
        return $list[$weekDay];
    }
    //What month and year do we want to show?
    //Ger it from URL or use current
    $month = $_GET['month'];
    if ($month == "") {
        $month = date('m');
    }
    $year = $_GET['year'];
    if ($year == "")  {
        $year = date('Y');
    }
    //Firts day of month
    $startDayStr = "01-$month-$year";
    //Some calculation to get interval of whole month and rounded weeks
    $startDay = strtotime($startDayStr) - (getfirstday($month, $year ) - 1) * 24*60*60;
    $roundMonth = 7 - getlastday($month, $year );
    $limit = cal_days_in_month(CAL_GREGORIAN, $month, $year ) + (getfirstday($month, $year ) - 1) + $roundMonth;
//Some navigation
    ?>
    <table>
        <tr>
          <?php
    if ($month > 1) {
                $prevm = $month - 1;
                $prevh = "cal.php?month=$prevm&year=$year";
            } elseif ($month == 1) {
                $prevm = 12;
                $prevy = $year -1;
                $prevh = "cal.php?month=$prevm&year=$prevy";
            }
            if ($month < 12) {
                $nextm = $month +1;
                $nexth = "cal.php?month=$nextm&year=$year";
            } elseif ($month == 12) {
              $nextm = 1;
              $nexty = $year +1;
              $nexth = "cal.php?month=$nextm&year=$nexty";
            }
            ?>
            <td width="200" align="left"><a href="<?php echo $prevh; ?>">&lt;Previous month</a></td>
            <td  width="190" align="center">
                <?php echo date('m', strtotime("01-$month-$year 00:00:00")); ?>
            </td>
            <td  width="200" align="right"><a href="<?php echo $nexth; ?>">Next month&gt;</a></td>
        </tr>
    </table>
    <?php
    //Let's generate our calendar
    echo "<table border=\"1\"><tr>";
    for($j=1;$j<=7;$j++){
        echo "<td align=\"center\" width=\"85\" height=\"50\"><b>".getweekday($j)."</b></td>";
    }
    echo "</tr><tr>";
    for($i = 1;$i <= $limit;$i++) {
        $lastDayOfMonth = strtotime(cal_days_in_month(CAL_GREGORIAN, $month, $year)."-$month-$year 23:59:59");
        $firstDayOfMonth = strtotime("01-$month-$year");
        $weekDay = date('N', $startDay);
        if ($startDay < $firstDayOfMonth || $startDay > $lastDayOfMonth) {
            $class = "caltdb";
        } else {
            $class = "caltda";
        }
        echo "<td class=\"$class\" align=\"center\" height=\"40\">". date('d', $startDay) ."</td>";


        if ($weekDay == '7') {
            echo "</tr><tr>";
        }
        $startDay = $startDay + 24*60*60;
    }
    echo "</tr></table>";
    ?>

この問題を解決するのを手伝ってくれませんか? なぜそれが起こっているのかわかりません。

どうもありがとう、Heretiiik

4

1 に答える 1

3

この問題は+ 24*60*60、タイムスタンプに 1 日を追加するために使用していることが原因である可能性があります。DST が開始/終了する 23 時間または 25 時間の日があるため、これは夏時間の問題を引き起こします。

スクリプトの終わり近くで、以下を置き換えます。

$startDay = $startDay + 24*60*60;

と:

$startDay = strtotime('+1 day', $startDay);
于 2013-08-06T00:54:53.700 に答える