列表中省略少于31天的月份 [英] Months with less than 31 days omitted from list

查看:244
本文介绍了列表中省略少于31天的月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下昨天工作良好的代码,但今天(3月30日),它已停止工作...

Consider the following code that was working perfectly yesterday, but now today (30th March), it has stopped working...

<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,在另一个查询中重新加载页面

This outputs a select list of upcoming months, that onchange, reloads the page with a different query in the URL for the calendar on that page to react on.

如果我手动更新网址中的查询,它可以正常工作,但是这个选择列表是重复日期,有一个看下面的图片...

If i manually update the query in the URL, it works fine, but this select list is repeating dates, have a look at the image below...

所有最短的几个月内的所有月份都会丢失,或事实上已被替换为与前一个月相同的名称,这不是巧合?

This cant be a coincidence that all of the months with the shortest days are missing, or in fact have been replaced with the same name as the month before?

有没有人有为什么会发生这种情况?

Does anyone have an idea why this might be happening?

推荐答案

这是因为当您在31日一个月你会遇到几个月不到30天的时间。 PHP会自动将日期调整到当月的最后一天,而是转到月底,然后再添加剩余的日期,并获取下个月的日期。

This is because when you loop through dates on a monthly basis on the 31st of the month you will come across months that have less than 30 days. PHP does automatically adjust the date to the last day of the month but instead goes to the end of the month and then adds the remaining days to it and gets the date that way which is the next month.

所以如果你到二月,只有28天(大多数年份),它将会到达28日,并添加三天,并给你3月3日。

So if you get to February, which only has 28 days (most years) it will get to the 28th and add three days and give you March 3rd.

要解决这个问题,您需要将日期设置为该月份的第一天,然后重复日期。

To solve this you need to set the date to the first day of the month and then iterate through the dates.

这篇关于列表中省略少于31天的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆