duanchifo2866 2013-08-05 20:14 采纳率: 100%
浏览 37
已采纳

我的PHP脚本用于在10月生成日历错误

I made a simple PHP script for generating calendar. It has rounded weeks (I mean if month starts on Friday it will generate it monday of that week), navigation, etc. Works great, but there is a bug in October. It draws last sunday of month twice. I use sundays as a singnal for new row so it makes

Example of my calendar

Example of calendar with October bug

And here the code (I'm not professional, I learned PHP on my own, with no books, just with google and PHP manual):

    <?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>";
    ?>

Could you help me fix this problem? I dont know why is it happening.

Thank you a lot, Heretiiik

  • 写回答

1条回答 默认 最新

  • doulu5717 2013-08-06 00:54
    关注

    The problem is likely due to the fact that you're using + 24*60*60 to add one day to a timestamp. This causes problems with daylight saving time, because there are days with 23 or 25 hours when DST begins/ends.

    Near the end of your script, replace:

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

    with:

    $startDay = strtotime('+1 day', $startDay);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题