【发布时间】:2018-07-28 16:46:44
【问题描述】:
如何将值存储在数组中的循环中并在 yii2 中以 JSON 形式返回。
我对这段代码有疑问:
$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;
【问题讨论】:
-
$array[] = $value -
请注意,使用 1 个月作为间隔可能会有问题。见example 3