2017-03-31 41 views
0

考虑下面的代码是可以正常使用昨天,但现在今日(3月30日),它已停止工作......个月少于31天,从列表中省略

<form class="date"> 
<select name="date" onchange="this.form.submit()"> 

<?php 
    for ($i = 0; $i <= 60; ++$i) { 
    $time = strtotime(sprintf('+%d months', $i)); 
    $value = date('Y-m', $time); 
    $label = date('F Y', $time); 
    $nowvalue = date('Y-m'); 
    $nowlabel = date('F Y'); 
     if(isset($_GET['date'])) { 
      if(strcmp($value,$_GET['date']) == 0) { 
      //If 0, $value and $_GET['date'] are the same: The option is selected 
      printf('<option value="%s" selected>%s</option>', $value, $label); 
      } else { 
      printf('<option value="%s">%s</option>', $value, $label); 
      } 
     } else { 
      if($value == $nowvalue) { 
      printf('<option value="%s" selected>%s</option>', $value, $label); 
      } else { 
      printf('<option value="%s">%s</option>', $value, $label); 
      } 
    } 
    } 
    ?> 
</select> 
</form> 

该输出的选择列表即将到来的几个月,onchange会在该页面上的日历的URL中使用不同的查询重新加载页面以作出反应。

如果我手动更新URL查询,它工作正常,但这种选择列表重复日期,看看下面的图片...

enter image description here

这不能是一个巧合在最短的日子里所有的月份都失踪了,或者实际上已经被替换为与前一个月相同的名字?

有没有人有一个想法,为什么这可能会发生?

+3

这些问题出现在本月的31号... –

回答

0

正如John Conde指出的那样,这是因为今天的日期(第31次)加上一个月是实际的5月1日,并且4月被跳过。

我做了以下的日期设置为每月的1日,然后加上一个月...

<?php 
    for ($i = 0; $i <= 60; ++$i) { 
    $firstofthismonth = '01-' . date('m-Y'); 
    $time = strtotime(sprintf($firstofthismonth . '+%d months', $i)); 
    $value = date('Y-m', $time); 
    $label = date('F Y', $time); 
    $nowvalue = date('Y-m'); 
    $nowlabel = date('F Y'); 
     if(isset($_GET['date'])) { 
      if(strcmp($value,$_GET['date']) == 0) { 
      //If 0, $value and $_GET['date'] are the same: The option is selected 
      printf('<option value="%s" selected>%s</option>', $value, $label); 
      } else { 
      printf('<option value="%s">%s</option>', $value, $label); 
      } 
     } else { 
      if($value == $nowvalue) { 
      printf('<option value="%s" selected>%s</option>', $value, $label); 
      } else { 
      printf('<option value="%s">%s</option>', $value, $label); 
      } 
    } 
    } 
    ?> 
+0

你应该能够将其标记为现在接受:) –

3

这是因为当你在月份的31号每月循环日期时,你会遇到不到30天的月份。 PHP会自动将日期调整到月份的最后一天,但会自动调整到月末,然后将剩余日期添加到月份中,并获取下个月的日期。

所以,如果你到了2月,只有28天(大部分时间),它将达到28日,并增加三天,并给你3月3日。

要解决此问题,您需要将日期设置为月份的第一天,然后遍历日期。

+0

感谢您的评论,我明白现在发生了什么,改变那条线虽然没有帮助。我会想到$ time = strtotime(sprintf('+%d months',$ i));会是罪魁祸首,因为它从3月31日开始的+1个月,如果30天是正确的,它会在5月1日发布。 – Collins

+0

我只是看着那个。不幸的是,您删除了可帮助确定在哪里进行更改的代码,但简而言之,只需将您正在迭代的日期并将其更改为本月的第一天即可。这将解决你的问题。 –

+0

谢谢,我现在想通了,会发布一个单独的答案。 – Collins

1

这仅仅是一个供参考,因为我知道你已经添加你自己的答案。

你可以改为使用DateTime()来简化你的代码(如果你愿意的话)。

所以,你可以用一些落得像:

<select name="date" onchange="this.form.submit()"> 

    <?php 

    $selected = DateTime::createFromFormat('Y-m-d', @$_GET['date'] . '-01') ?: new DateTime('first day of this month'); 

    for ($i = 0; $i <= 60; $i++) { 

     $date = (new DateTime('first day of this month'))->modify("+$i months"); 

     $select = $date == $selected ? 'selected' : ''; 

     printf('<option value="%s" %s>%s</option>', $date->format('Y-m'), $select, $date->format('F-Y')); 

    } 
    ?> 

</select> 

希望这有助于!

相关问题