ある日付$date_from
から$date_to
までの1月間隔の日付のリストを作る方法.
use Cake\I18n\Date; // 抜粋 public function date_arange_month($date_from, $date_to) $date_list = []; for($date_item = clone $date_from; $date_item->lte($date_to); $date_item->addMonth(1)){ $date_list[] = clone $date_item; } $this->log($date_list); }
このように書くと以下のようなリストが得られます。
$date_from = new Date("01/01 -1 year"); $date_to = new Date(); $this->log(date_arange_month($date_from, $date_to));
出力
2019-08-20 12:06:10 Error: Array ( [0] => Cake\I18n\Date Object ( [time] => 2018-01-01T00:00:00+00:00 [timezone] => UTC [fixedNowTime] => ) [1] => Cake\I18n\Date Object ( [time] => 2018-02-01T00:00:00+00:00 [timezone] => UTC [fixedNowTime] => ) [2] => Cake\I18n\Date Object ( [time] => 2018-03-01T00:00:00+00:00 [timezone] => UTC [fixedNowTime] => ) [3] => Cake\I18n\Date Object ( [time] => 2018-04-01T00:00:00+00:00 [timezone] => UTC [fixedNowTime] => ) [4] => Cake\I18n\Date Object ( [time] => 2018-05-01T00:00:00+00:00 [timezone] => UTC [fixedNowTime] => ) // 続く