dqzuo0327 2019-01-29 07:17
浏览 66
已采纳

当我去下个月时,日历中缺少几个月

I have a calendar using PHP and I have a problem going to the next month like for example today's month is JANUARY when I go to the next month instead of showing FEBRUARY it's showing the month of MARCH, I didn't have this problem until January 29, I did some test to find the solution to my problem by changing the date, when I change my date to Jan 28, my code works perfectly fine, I think the problem is that my calendar is searching for a month, day, year and because some months don't have 31 as their day and February doesn't have 29,30,31 as its day it's overlooking some months.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script>
        function goLastMonth(month, year){
        if(month == 1){
            --year;
        month = 13;
        }
document.location.href = "<?php $_SERVER['PHP_SELF'];?>?month="+(month-1)+"&year="+year;
        }
    function goNextMonth(month, year){
        if(month == 12){
            ++year
        month = 0;
        }
document.location.href = "<?php $_SERVER['PHP_SELF'];?>?month="+(month+1)+"&year="+year; 
    }
    </script>
</head>
<body>
    <?php
        if(isset($_GET['day'])){
        $day = $_GET['day'];
    }else{
        $day = date("j");
        }  
    if(isset($_GET['month'])){
        $month = $_GET['month'];
    }else{
        $month = date("n"); 
    }
    if(isset($_GET['year'])){
        $year = $_GET['year'];
    }else{
        $year = date("Y");
    }
    date_default_timezone_set('Asia/Manila');
    $currentTimeStamp = strtotime("$year-$month-$day");
    $monthName = date("F", $currentTimeStamp);
    $numDays = date("t", $currentTimeStamp);
    $counter = 0;
    ?>
    <table border="1">
        <tr>
        <td><input style='width:50px;' type='button' value='<' name='previousbutton' onclick="goLastMonth(<?php echo $month.",".$year?>)"> 
           </td>
       <td colspan="5"><?php echo $monthName. "," .$year;?></td>
            <td><input style='width:50px;' type='button' value='>' name='nextbutton' onclick="goNextMonth(<?php echo $month.",".$year?>)"></td>
    </tr>
        <tr>
            <td style="width:50px;">Sun</td>
        <td style="width:50px;">Mon</td>
        <td style="width:50px;">Tue</td>
        <td style="width:50px;">Wed</td>
        <td style="width:50px;">Thu</td>
        <td style="width:50px;">Fri</td>
        <td style="width:50px;">Sat</td>
       </tr>
      <?php  
      echo "<tr>";
      for($i = 1; $i < $numDays+1; $i++, $counter++){
          $timeStamp = strtotime("$year-$month-$i");
          if($i == 1){
              $firstDay = date("w", $timeStamp);
          for($j = 0; $j < $firstDay; $j++, $counter++){
              echo "<td>&nbsp;</td>";
          }
          }
          if($counter % 7 == 0){
              echo "</tr><tr>";
          }
          echo "<td align='center'>".$i."</td>";
      } 
      echo "</tr>";
      ?>
   </table>
</body>
</html>

Let's say today's month is January and I'm expecting when I select the next month it will go to February, not March.

  • 写回答

1条回答 默认 最新

  • douping1581 2019-01-29 08:34
    关注

    You are correct that the problem stems from the available number of days in the next month.

    Since you are using the current day as a base for $currentTimeStamp, you get unexpected results, not unlike those mentioned here: http://php.net/manual/en/function.strtotime.php#107331

    If you want to keep using the current date in your calendar, you need to handle this problem somehow. I suggest that if today's date is larger than the number of days in the next month, you instead use the last day of that month:

    $currentTimeStamp = strtotime("$year-$month");
    if($day > date("t", $currentTimeStamp) {
      $day = date("t", $currentTimeStamp);
    }
    $currentTimeStamp = strtotime("$year-$month-$day");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常