【问题标题】:How to get month name of custom range如何获取自定义范围的月份名称
【发布时间】:2021-10-06 12:25:58
【问题描述】:

在我的 laravel 应用程序中, 输入是这样的:

$from_year = 2019;
$from_month = 10;

$to_year = 2020;
$to_month = 4;

预期输出:

months = [
'October 2019', 'November 2019', 'December 2019', 'January 2020', 'February 2020', 'March 2020', 'April 2020'
]

提前致谢。

【问题讨论】:

    标签: php laravel date carbon


    【解决方案1】:

    您可以使用几种使用DateTime 的方法。首先,您可以创建当前和结束日期(每月的第一天)对象,然后使用date_diff 计算月差。然后添加月份间隔就很简单了。

    普通 PHP 解决方案如下所示:

    $from_year = 2019;
    $from_month = 10;
    
    $to_year = 2020;
    $to_month = 4;
    
    $date = new DateTime("$from_year-$from_month-01");
    $diff = date_diff($date, new DateTime("$to_year-$to_month-01"));
    $monthsDiff = $diff->y * 12 + $diff->m;
    $interval = new DateInterval('P1M');
    
    $monthsArray = [];
    
    for($i = 0; $i <= $monthsDiff; $i++){
        $monthsArray[] = $date->format('F Y');
        $date->add($interval);
    }
    

    另外,如您所见,我在DateTime 对象上使用format 方法。更多方法可以阅读here

    【讨论】:

      【解决方案2】:
      $startDate = date($from_year.'-'.$from_month.'-01');
      $diff = (($to_year - $from_year) * 12) + ($to_month - $from_month);
      
      $months = [];
      for($i=0;$i<=$diff;$i++) {
          $months[] = date('F Y', strtotime('+'.$i.' month', strtotime($startDate)));
      }
      

      带有date()函数的简单代码

      【讨论】:

        【解决方案3】:

        试试下面的代码

           <?php
        
        $from_year = 2019;
        $from_month = 10;
        
        $to_year = 2020;
        $to_month = 4;
        $arr = [];
        for($i=$from_year;$i<$to_year;$i++) {
            
            if($i==$from_year){
                $fmonth = $from_month;
            }else{
                $fmonth = 1;
            }
            for($j=$fmonth;$j<=12;$j++){
                array_push($arr,DateTime::createFromFormat('!m', $j)->format('F'). " ".$i);
            }
        }
            for($i=1;$i<=$to_month;$i++){
                array_push($arr,DateTime::createFromFormat('!m', $i)->format('F'). " ".$to_year);
            }
        
        print_r($arr);
        ?>
        

        【讨论】:

          【解决方案4】:

          这可以使用 Carbon 和 CarbonPeriod 库来完成。

          事情是这样的:

          use Carbon\Carbon;
          use Carbon\CarbonPeriod;
          
          $from_year = 2019;
          $from_month = 10;
          
          $to_year = 2020;
          $to_month = 4;
          
          $fromDate = new Carbon('1-' . $from_month . '-' . $from_year);
          $toDate = new Carbon('1-' . $to_month . '-' . $to_year);
          
          $differnces = CarbonPeriod::create($fromDate, '1 month', $toDate);
          
          $differenceInDates = [];
          
          foreach ($differnces as $differnce) {
              $differenceInDates[] = $differnce->format("M F");
          }
          

          这会给你想要的结果。

          【讨论】:

            【解决方案5】:
            $from_year = 2019;
            $from_month = 10;
            
            $to_year = 2020;
            $to_month = 4;
            
            $start = strtotime($from_year . "-" . $from_month . "-01");
            $end = strtotime($to_year . "-" . $to_month . "-01");
            $months = [date("F Y",$start)];
            
            while(strtotime(end($months)) < $end){
                $months[] = date("F Y",strtotime(end($months) . " +1 month"));
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-01-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-03-12
              相关资源
              最近更新 更多