-1

how can i store the values inside a loop in array and return as JSON in yii2.

I have problem with this code:

$start = date("Y/m/d");
            $interval = DateInterval::createFromDateString('1 month');
            $end = date('Y/m/d', strtotime(sprintf("+%d months", $loanMonths)));
            $periods = new DatePeriod(new DateTime($start), $interval, new DateTime($end));
            $array = array();
            foreach($periods as $period){
                print_r(date('Y-F', strtotime($period->format('Y-m')))); echo "<br>";
            }
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
            return $period;
ldg
  • 9,112
  • 2
  • 29
  • 44
Alesh
  • 341
  • 1
  • 4
  • 20

1 Answers1

0

I think this is what you need

 $array = array();
 foreach($periods as $period){
    $array []= date('Y-F', strtotime($period->format('Y-m')));
 }

 $response = Yii::$app->response;
 $response->format = \yii\web\Response::FORMAT_JSON;
 $response->data = $array;

 return $response;
Shobi
  • 10,374
  • 6
  • 46
  • 82