【问题标题】:Custom Month and Year inside php for-loopphp for-loop中的自定义月份和年份
【发布时间】:2018-08-08 09:43:26
【问题描述】:

我看到了这个问题: how to get the previous 3 months in php

我的问题是.. 我如何从自定义月份输出。
我想从Mar 2018(或任何M Y 用户输入)开始,它应该输出下一个3(或任何数量的用户输入)月份。
例如:MarAprMay

以下代码来自当前月份和年份。

// current month: Aug 2018
for ($i = 0; $i <= 2; $i++){
    $x = strtotime("$i month");
    echo $dte = date('M Y', $x);
    echo '<br>';
}

输出是

Aug 2018
Sep 2018
Oct 2018

【问题讨论】:

  • 您可以尝试如下更改,您可以在其中获取动态月份和年份并获得结果 $effective_date = "MAR 2018"; $x = strtotime("$i 月",strtotime($effective_date));

标签: php datetime for-loop


【解决方案1】:

您可以使用DateTime 类并使用DateInterval 对象递增:

// Assuming these are the user inputs
$month = 11;
$year = 2015;

// We create a new object with year and month format
$date = DateTime::createFromFormat('Y m', $year . ' ' . $month);


for ($i = 0; $i <= 2; $i++){
    // Output the month and year
    echo $date->format('m Y') . '<br>';

    // Add 1 month to the date
    $date->add(new DateInterval('P1M'));
}

输出:

2015 年 11 月
12 2015
01 2016

文档:

【讨论】:

  • @MagnusEriksson 是的,这会自动更改年份(使用 PHP 7.2 从Dec 2015 开始测试:工作)。
  • 当然可以。您正在使用DateInterval。我收回我之前的评论 :-) +1 使用 DateTimeDateInterval
【解决方案2】:

将其更改为如下所示,它将按预期为您提供,请参见下面的代码

    // current month: Aug 2018
  $effective_date = "MAR 2018";
for ($i = 0; $i <= 2; $i++){
    $x = strtotime("$i month",strtotime($effective_date));
    echo $dte = date('M Y', $x);
    echo '<br>';
}

【讨论】:

    【解决方案3】:

    这可能也有帮助:

    <?php
    $month = 11;
    $year  = 2017;
    $count = 15;
    
    for ($i = 1; $i <= $count; $i++) {
        $month++;
        if ($month > 12) {
            $month = 1;
            $year++;    
        }   
        $x = DateTime::createFromFormat('Y-m', $year.'-'.$month);
        echo $x->format('m-Y');
        echo '<br>';
    }
    ?> 
    

    输出:

    12-2017
    01-2018
    02-2018
    03-2018
    04-2018
    05-2018
    06-2018
    07-2018
    08-2018
    09-2018
    10-2018
    11-2018
    12-2018
    01-2019
    02-2019
    

    【讨论】:

    • 为什么是 15 个结果?为什么使用DateTime,但手动添加月份而不是使用DateTime::add / date_add()
    • @AymDev 只是另一种方法。谢谢!
    【解决方案4】:

    尝试使用此代码添加第 n 天、月和年

    $n = 2;
    for ($i = 0; $i <= $n; $i++){
        $d = strtotime("$i days");
        $x = strtotime("$i month");
        $y = strtotime("$i year");
        echo "Dates : ".$dates = date('d M Y', "+$d days");
        echo "<br>";
        echo "Months : ".$months = date('M Y', "+$x months");
        echo '<br>';
        echo "Years : ".$years = date('Y', "+$y years");
        echo '<br>';
    }
    

    【讨论】:

      【解决方案5】:

      您可以使用strtotime()函数生成日期

      检查这个phpfiddle

      <?php
      $currMonth = 11;
      $currYear  = 2017;
      $count = 15;
      $currentYear = date('M Y',strtotime($currYear.'-'.$currMonth));
      echo $currentYear.'<br>';
      for ($i = 1; $i < $count; $i++) {
          $currMonth++;
          if ($currMonth > 12) {
              $currMonth = 1;
              $currYear++;    
          }   
          $newMonthandYear = date('M Y',strtotime($currYear.'-'.$currMonth));
          echo $newMonthandYear.'<br>';
      }
      ?> 
      

      【讨论】:

        【解决方案6】:
        <?php
        
        $year = date('Y');
        
        //start at march 
        $startMonth = 7;
        
        //go forwards 2 months
        $stepForwards = 22;    
        
        for ($i = $startMonth; $i <= $stepForwards; $i++){
        
            if($i > 12 ) {
        
                $month = $i % 12 == 0 ?  12 : $i % 12;
        
            }
            else {
        
               $month = $i;
        
            }
        
            echo date("M Y", strtotime(date($year.'-'.$month.'-1'))) . "<br/>";
        
            if($month == 12) {
        
                $year++;
        
                echo "<br/>";
        
            }
        
        }
        

        【讨论】:

        • 这不会输出任何东西,因为$startMonth 已经大于$stepForwards
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-17
        • 1970-01-01
        • 2022-01-25
        • 1970-01-01
        • 2014-11-26
        • 1970-01-01
        相关资源
        最近更新 更多