【问题标题】:php - for loop for each month of yearphp - 一年中每个月的 for 循环
【发布时间】:2013-08-08 09:29:20
【问题描述】:

我想要一个循环来检查当前月份、未来 12 个月和过去 4 个月。

例如:今天是 08 年 8 月 1 日。我的循环应该经过 4 月、5 月、6 月、7 月、8 月、9 月、10 月、11 月、12 月、1 月、2 月、3 月、4 月、5 月、6 月、7 月和八月。

我尝试过 strotime,但我不知道如何循环回溯 4 个月和未来 12 个月。

这是我的代码

$i = 1; 
$month = strtotime('2013-08-01');

    while($i <= 12) {
        $month_name = date('F', $month);
        echo $month_name;
        echo "<br>";

        $month = strtotime('+1 month', $month);
        $i++;

【问题讨论】:

  • 从四个月前的时间戳开始,将循环扩展到 16 个月将是一个简单的方法

标签: php date loops for-loop


【解决方案1】:

我认为 Yoshi 和 his answer 差不多,但使用 DatePeriodDateTime 更一致,并且恕我直言,代码可读性更强:-

$oneMonth = new \DateInterval('P1M');
$startDate = \DateTime::createFromFormat('d H:i:s', '1 00:00:00')->sub(new \DateInterval('P4M'));
$period = new \DatePeriod($startDate, $oneMonth, 16);

foreach($period as $date){
    //$date is an instance of \DateTime. I'm just var_dumping it for illustration
    var_dump($date);
}

See it working

【讨论】:

  • +1 不错,虽然我个人会删除一些一次性使用变量。
【解决方案2】:

这可能很棘手,我会这样做:

$month = date("n", "2013-08-01") - 1; // -1 to get 0-11 so we can do modulo

// since you want to go back 4 you can't just do $month - 4, use module trick:
$start_month = $month + 8 % 12;
// +8 % 12 is the same is -4 but without negative value issues
// 2 gives you: 2+8%12 = 10 and not -2

for ($i = 0; $i < 16; $i += 1) {
    $cur_month = ($start_month + $i) % 12 + 1; // +1 to get 1-12 range back
    $month_name = date('F Y', strtotime($cur_month . " months"));
    var_dump(month_name);
}

【讨论】:

    【解决方案3】:

    像这样?:

    $start = -4;
    $end = 12;
    
    for($i=$start; $i<=$end;$i++) {
        $month_name = date('F Y', strtotime("$i months"));
        echo $month_name;
        echo "<br>";
    }
    

    【讨论】:

      【解决方案4】:

      您的代码,只是稍作修改。

      date_default_timezone_set('UTC');
      
      $i = 1;
      $month = strtotime('-4 month');
      
      while($i <= 16) {
          $month_name = date('F', $month);
          echo $month_name;
          echo "<br>";
      
          $month = strtotime('+1 month', $month);
          $i++;
      }
      

      【讨论】:

        【解决方案5】:

        最简单的解决方案:

        for($i=-4; $i<=12; $i++) {
            echo date("F",strtotime( ($i > 0) ? "+$i months" : "$i months") )."\n";
        }
        

        解释:

        循环从 -4 开始,一直到 12(总共 17,包括 0)。 strtotime() 中的三元语句只是检查 $i 是否为正,如果是,则插入 +,以便我们得到 strtotime("+1 months") 和类似的结果。

        Ta-da!

        【讨论】:

        • 如何将其修改为从 1 月到 12 月循环?
        • @Xitish 你可以调整循环变量。如果您想要可读的代码,您还可以创建一个月份列表并将月份名称存储在那里。如果你想用代码来做,你也可以这样做:$months = array_map(function ($i) { return date('F', mktime(0, 0, 0, $i, 1)); }, range(1, 12));.
        【解决方案6】:

        使用DateTime 是最简单且更易读的方法。 我会这样做:

        $from = new DateTime('-4 month');
        $to   = new DateTime('+12 month');
        while($from < $to){
          echo $from->modify('+1 month')->format('F');
        }
        

        【讨论】:

          猜你喜欢
          • 2011-10-24
          • 2015-12-09
          • 1970-01-01
          • 1970-01-01
          • 2021-12-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多