【问题标题】:Loop to create list of previous 12 months循环创建前 12 个月的列表
【发布时间】:2014-05-15 20:51:01
【问题描述】:

有没有一种方法可以使用 PHP 循环根据当前月份(不包括当前月份)创建一个与前 12 个月类似的列表?

该值应始终为月份的第一天(格式:yyyy-mm-dd),下拉列表本身应仅显示年份和月份(格式:yyyy-mm):

<option value="2014-03-01">2014-03</option>
<option value="2014-02-01">2014-02</option>
<option value="2014-01-01">2014-01</option>
<option value="2013-12-01">2013-12</option>
<option value="2013-11-01">2013-11</option>
<option value="2013-10-01">2013-10</option>
//...

我尝试了以下方法,但似乎有问题,因为这不起作用:

<?php for ($i=0; $i<=12; $i++) { ?>
    <option value="<?php echo date('Y-m-d', strtotime("-1 month")); ?>"><?php echo date('Y-m', strtotime("-1 month")); ?></option>
<? } ?>

【问题讨论】:

  • 谢谢。以这种方式使用它时,我得到以下信息:解析错误:语法错误,意外 $end... 我可以将 $i 月用于两种日期格式吗?
  • 添加为下面的回答者
  • 谢谢 - 我在下面回复了。

标签: php date for-loop foreach


【解决方案1】:
$start    = (new DateTime('1 year ago'))->modify('first day of this month');
$end      = (new DateTime())->modify('first day of this month');
$interval = new DateInterval('P1M');
$period   = new DatePeriod($start, $interval, $end);

$months = array();
foreach ($period as $dt) { 
    $months[$dt->format('Y-m-d')] = $dt->format('Y-m');
}
$reverse_months = array_reverse($months);
print_r($reverse_months);

Demo

然后您可以循环访问 $reverse_months 来创建您的下拉菜单

foreach($reverse_months as $key => $value) {
?>
    <option value="<?php echo key; ?>"><?php echo value; ?></option>
<?php
}

我们之所以必须使用array_reverse(),是因为DatePeriod只会在时间上向前推进。

【讨论】:

  • 也谢谢你。我将使用上面的,因为这需要添加的代码更少。
【解决方案2】:

将 -1 更改为 $i 以反映并存储它以供双重使用。

<?php 
    for ($i=1; $i>=12; $i++) { 
        $last = strtotime("-$i month");
?>
    <option value="<?php echo date('Y-m-d', $last);?>"><?php echo date('Y-m', $last);?></option>
 <? } ?>

【讨论】:

  • 这不是 op 想要的
【解决方案3】:
<?php
for ($i=0; $i<=12; $i++) { 
echo '<option value="'.date('Y-m-d', strtotime("-$i month")).'">'.date('Y-m', strtotime("-$i month")).'</option>';
 } 

【讨论】:

  • 这很完美。它缺少 PHP 关闭,但除此之外它很棒 - 非常感谢!
  • 它没有丢失,它不是必需的,在这种情况下它是“文件”中的唯一代码
  • 当当前日期大于上个月的天数时,这将不起作用...
  • 之前的评论是正确的:不要使用这个解决方案!该错误仅在每月的第 31 天可见
【解决方案4】:

您可以使用datetime 扩展来创建迭代器。很简单:

// This is when to start counting
$start = new DateTime('now');

// The interval; i.e. every time we iterate we should get
// the first day of the previous month
$interval = DateInterval::createFromDateString('first day of last month');

// The period (or the iterator) should go for twelve
// months and the start date should not be included
$period = new DatePeriod($start, $interval, 12, DatePeriod::EXCLUDE_START_DATE);

// The DatePeriod class implements the Traversable
// interface and can therefore be used in a foreach loop
foreach($period as $time) {
    $val = $time->format("Y-m-d");
    $txt = $time->format("Y-m");
    echo "<option value=\"{$val}\">{$txt}</option>\n";
}

【讨论】:

    【解决方案5】:

    这是我用过的,它有点初级,但即使你到了月底也能工作..不像 Glavić 指出的公认答案:

    <?php
    $year = date("Y");
    $month = date("m");
    for ($i=0; $i<=12; $i++)
    {
        $month--;
        if ($month < 1) { $month = 12; $year--; }
        echo '<option value="'.date('Y-m-d', strtotime($year."-".$month."-01")).'">'.date('Y-m', strtotime($year."-".$month."-01")).'</option>';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 2014-05-05
      • 2022-07-12
      • 2013-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多