Php 日期格式M-Y

Php 日期格式M-Y,php,symfony,date,Php,Symfony,Date,我正在尝试使用格式(“m-Y”)查找格式为'm-Y'的12个最新日期,但出现了如下错误: 类:“Symfony\Component\Debug\Exception\FatalThrowableError” 消息:“对字符串调用成员函数format()” 我已经试过了: $date = new DateTime(); $mois = $date->format('M-Y'); $mois1 = $date->format('Y-m-d'); array_push($format1, $

我正在尝试使用
格式(“m-Y”)查找格式为
'm-Y'
的12个最新日期,但出现了如下错误:

类:“Symfony\Component\Debug\Exception\FatalThrowableError”
消息:“对字符串调用成员函数format()”

我已经试过了:

$date = new DateTime();
$mois = $date->format('M-Y');
$mois1 = $date->format('Y-m-d');
array_push($format1, $mois);
array_push($format2, $mois1);
结果不是我所期望的。我得到了2019年6月的
12次

我的代码是:

public function getMonthLibelleByDates($filtre) {
        $format1 = []; $format2 = [];
        $month = time();
        for ($i = 1; $i <= 12; $i++) {
            $month = strtotime('last month', $month);
            $months[] = date("r", $month);
        }
        foreach($months as $mois) {
            array_push($format1, $mois->format('M-Y'));
            array_push($format2, $mois->format('Y-m-d'));
        }
        $response = array(
            'format1'=> $format1,
            'format2' =>  $format2      
        );
        return $response;  
    }
公共函数getMonthLibelleByDates($filter){
$format1=[];$format2=[];
$month=时间();
对于($i=1;$i格式('M-Y'));
数组推送($format2,$mois->format('Y-m-d');
}
$response=array(
'format1'=>$format1,
'format2'=>$format2
);
返回$response;
}

我预计输出是从当前日期算起的最近12个月。

您似乎在混合时间戳、
date()
字符串和DateTime对象

您可以做的是从今天开始创建一个DateTime对象,然后在12次迭代循环中修改它以减去一个月。在每次迭代中添加格式,然后返回响应

public function getMonthLibelleByDates($filtre) {
    $format1 = []; 
    $format2 = [];
    $date = new DateTime();

    for ($i = 1; $i <= 12; $i++) {
        $format1[] = $date->format("M-Y");
        $format2[] = $date->format("Y-m-d");
        $date->modify("-1 month");
    }

    return array(
        'format1'=> $format1,
        'format2' =>  $format2      
    );
}
公共函数getMonthLibelleByDates($filter){
$format1=[];
$format2=[];
$date=新的日期时间();
对于($i=1;$i格式(“M-Y”);
$format2[]=$date->format(“Y-m-d”);
$date->modify(“-1个月”);
}
返回数组(
'format1'=>$format1,
'format2'=>$format2
);
}
作为旁注,似乎未使用参数
$filter
,因此可能会将其删除

  • 现场演示

    • 有一个最简单的方法来实现这一点

      var_dump(getMonthLibelleByDates());
      
      function getMonthLibelleByDates() {
          $res = [
              'format1' => [],
              'format2' => []
          ];
          $timestampBuffer = NULL;
      
          for ($i = 1; $i <= 12; $i++) {
              $timestampBuffer = strtotime("-$i month");
      
              $res['format1'][] = date('M-Y', $timestampBuffer);
              $res['format2'][] = date('Y-m-d', $timestampBuffer);
          }
      
          return $res;
      }
      

      date
      返回字符串,而不是DateTime对象。
      array(2) {
          ["format1"]=>
        array(12) {
              [0]=>
          string(8) "May-2019"
              [1]=>
          string(8) "Apr-2019"
      
          //... (too long)
      
              [10]=>
          string(8) "Jul-2018"
              [11]=>
          string(8) "Jun-2018"
        }
        ["format2"]=>
        array(12) {
              [0]=>
          string(10) "2019-05-26"
              [1]=>
          string(10) "2019-04-26"
      
          //... (too long)
      
              [10]=>
          string(10) "2018-07-26"
              [11]=>
          string(10) "2018-06-26"
        }
      }