【问题标题】:My PHP script for generating calendar bugs in october我用于在 10 月生成日历错误的 PHP 脚本
【发布时间】:2013-08-06 08:54:53
【问题描述】:

我制作了一个简单的 PHP 脚本来生成日历。它有四舍五入的星期(我的意思是如果一个月从星期五开始,它将在该周的星期一生成它)、导航等。效果很好,但是 10 月份有一个错误。它在每月的最后一个星期日绘制两次。我使用星期天作为新行的信号,所以它使

Example of my calendar

Example of calendar with October bug

这里是代码(我不是专业的,我自学PHP,没有书,只有google和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>";
    ?>

你能帮我解决这个问题吗?我不知道为什么会这样。

非常感谢, 异端

【问题讨论】:

  • 示例链接返回 403 Forbidden 错误。
  • 需要修复您的示例链接,或在此处发布图片。我复制了你的代码,只更改了 $_GET['m']=>10 和 $_GET['y']=>2013 并且你的代码在这个 phpFiddle 中看起来很好 - phpfiddle.org/main/code/gbg-86q
  • 这个问题可能更适合codereview.stackexchange.com
  • @ironcito 已修复,我忘了设置权限

标签: php calendar


【解决方案1】:

问题可能是由于您使用+ 24*60*60 将一天添加到时间戳。这会导致夏令时出现问题,因为 DST 开始/结束时有 23 或 25 小时。

在你的脚本接近尾声时,替换:

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

与:

$startDay = strtotime('+1 day', $startDay);

【讨论】:

    猜你喜欢
    • 2017-07-11
    • 2020-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多