如果它落在2个月之间,则无法捕获一周

时间:2014-08-11 03:15:24

标签: php jquery date

这可能听起来很奇怪,但我只是想问我哪里出错了。我捕捉了我称之为周期的一周,但是当我选择最后一周时,例如7月27日至8月2日的7月上周。我可以捕获7月27日至7月31日,但无法捕获1和2

这是我的代码,请在我出错的地方提出建议。

这是我几个月的代码:

<label for="month" style="padding-right: 10px;margin-top:7px;">Month</label>
          <select name="month" id="month" class="text ui-widget-content ui-corner-all">
                <option value="0">Choose Month</option>
                <option value="January">January</option>
                <option value="February">February</option>
                <option value="March">March</option>
                <option value="April">April</option>
                <option value="May">May</option>
                <option value="June">June</option>
                <option value="July">July</option>
                <option value="August">August</option>
                <option value="September">September</option>
                <option value="October">October</option>
                <option value="Novermber">Novermber</option>
                <option value="December">December</option>
          </select>

这是我一周或一段时间的代码

    <label for="period" style="padding-right: 10px;margin-top:7px;" visible="false">Period</label>
          <select name="period" id="period" class="text ui-widget-content ui-corner-all">
                <option value="">Choose Period</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
          </select>

和年份

<select name="year" id="year" class="text ui-widget-content ui-corner-all">
                <?php
                    $year = date("Y");
                    $start = $year - 100;
                    for ($i = $start; $start <= $year; $start++) {
                        echo '<option value="' . $start. '">' . $start. '</option>';
                    }
                ?>
          </select>

如果我在7月份选择7月份,在7月份选择5年份,那么这一年结果

enter image description here

Asnwer必须是7月27日至8月2日,所以答案必须 2014年7月27日至2日。我怎样才能解决这个问题。谢谢你的帮助。

对此抱歉,以下是由jquery组成的过程

$(function(){
        $("#period").change(function(){
          var dateStr = "";
          var month = $('#month').val();
          var monthToDisplay = $('#month option[value='+month+']').text();
          var period = $('#period').val();
          var year = $('#year').val();
          var date = new Date(month+'/01/'+year);
          var day = date.getDay();
          var diff = 7 - parseInt(day);
          var start = 1;
          var end = 31;
          var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
            //alert(lastDay);
          if(period ==1)
          {
              end = diff;
          }
          else
          {
             start = diff + 7 * (parseInt(period) - 2)
            end = start+7;
            if(parseInt(end) > parseInt(lastDay))
               end = lastDay;
          }
           dateStr = monthToDisplay+', '+start+' - '+end+' '+year;
          $('#dateStr').html(dateStr);
        });

        $("#year").change(function(){
          var dateStr = "";
          var month = $('#month').val();
          var monthToDisplay = $('#month option[value='+month+']').text();
          var period = $('#period').val();
          var year = $('#year').val();
          var date = new Date(month+'/01/'+year);
          var day = date.getDay();
          var diff = 7 - parseInt(day);
          var start = 1;
          var end = 31;
          var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
            //alert(lastDay);
          if(period ==1)
          {
              end = diff;
          }
          else
          {
             start = diff + 7 * (parseInt(period) - 2)
            end = start+7;
            if(parseInt(end) > parseInt(lastDay))
               end = lastDay;
          }
           dateStr = monthToDisplay+', '+start+' - '+end+' '+year;
          $('#dateStr').html(dateStr);
        });

        $("#month").change(function(){
          var dateStr = "";
          var month = $('#month').val();
          var monthToDisplay = $('#month option[value='+month+']').text();
          var period = $('#period').val();
          var year = $('#year').val();
          var date = new Date(month+'/01/'+year);
          var day = date.getDay();
          var diff = 7 - parseInt(day);
          var start = 1;
          var end = 31;
          var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
            //alert(lastDay);
          if(period ==1)
          {
              end = diff;
          }
          else
          {
             start = diff + 7 * (parseInt(period) - 2)
            end = start+7;
            if(parseInt(end) > parseInt(lastDay))
               end = lastDay;
          }
           dateStr = monthToDisplay+', '+start+' - '+end+' '+year;
          $('#dateStr').html(dateStr);
        });
    });

1 个答案:

答案 0 :(得分:0)

这是我为解决问题所做的工作,但显示了另一个问题。

这是修改过的JS解决了我的问题。

$(function(){
        $("#period").change(function(){
          var dateStr = "";
          var month = $('#month').val();
          var monthToDisplay = $('#month option[value='+month+']').text();
          var period = $('#period').val();
          var year = $('#year').val();
          var date = new Date(month+'/01/'+year);
          var day = date.getDay();
          var diff = 7 - parseInt(day);
          var start = new Date(date.getTime() - 60*60*24* day*1000);
          var end = 31;
          //var lastDay = new Date(date.getFullYear(), date.getMonth(), 0).getDate();
          var lastDay = new Date(date.getTime() + 60 * 60 *24 * 6 * 1000);
            //alert(lastDay);
          if(period ==1)
          {
              end = diff;
          }
          else
          {
             start = diff + 7 * (parseInt(period) - 2)
             start = (parseInt(start + 1))
            end = start+6;
            if(parseInt(end) > parseInt(lastDay))
               end = lastDay;
          }
           dateStr = monthToDisplay+', '+start+' - '+end+' '+year;
           // dateStr = start+' - '+monthToDisplay+' , '+end+' '+year;
          $('#dateStr').html(dateStr);
        });

        $("#year").change(function(){
          var dateStr = "";
          var month = $('#month').val();
          var monthToDisplay = $('#month option[value='+month+']').text();
          var period = $('#period').val();
          var year = $('#year').val();
          var date = new Date(month+'/01/'+year);
          var day = date.getDay();
          var diff = 7 - parseInt(day);
          var start = new Date(curr.getTime() - 60*60*24* day*1000);
          var end = 31;
          //var lastDay = new Date(date.getFullYear(), date.getMonth(), 0).getDate();
          var lastDay = new Date(curr.getTime() + 60 * 60 *24 * 6 * 1000);
            //alert(lastDay);
          if(period ==1)
          {
              end = diff;
          }
          else
          {
             start = diff + 7 * (parseInt(period) - 2)
             start = (parseInt(start + 1))
            end = start+6;
            if(parseInt(end) > parseInt(lastDay))
               end = lastDay;
          }
           dateStr = monthToDisplay+', '+start+' - '+end+' '+year;
           //dateStr = start+' - '+monthToDisplay+' , '+end+' '+year;
          $('#dateStr').html(dateStr);
        });

        $("#month").change(function(){
          var dateStr = "";
          var month = $('#month').val();
          var monthToDisplay = $('#month option[value='+month+']').text();
          var period = $('#period').val();
          var year = $('#year').val();
          var date = new Date(month+'/01/'+year);
          var day = date.getDay();
          var diff = 7 - parseInt(day);
          var start = new Date(curr.getTime() - 60*60*24* day*1000);
          var end = 31;
          //var lastDay = new Date(date.getFullYear(), date.getMonth(), 0).getDate();
          var lastDay = new Date(curr.getTime() + 60 * 60 *24 * 6 * 1000);
          if(period ==1)
          {
              end = diff;
              //alert(day);
          }
          else if (period !==1 && period !== 6)
          {
             start = diff + 7 * (parseInt(period) - 2)
             start = (parseInt(start + 1))
            end = start+6;
            if(parseInt(end) > parseInt(lastDay))
               end = lastDay;
          }

           dateStr = monthToDisplay+', '+start+' - '+end+' '+year;
           //dateStr = start+' - '+monthToDisplay+' , '+end+' '+year;
          $('#dateStr').html(dateStr);
        });
    });

但这也显示了GMT的一些东西。这是一个示例答案。希望它对那里的一些海报有所帮助。对于其他人,只需要弄清楚时间和GMT

enter image description here