$first_date='2015-1-1'; $end_date = date('Y-m-d'); $month_range=4; $format='Y-m-d H:i:s'; $res = splitByMonthRange($first_date,$end_date,$month_range,$format); echo var_dump($res); /** * 期間を指定月間で分割 * * @param string/date $first_date 期間の開始日(月の第一日) * @param string/date $end_date 期間の終了日 * @param int $month_range 指定月間 * @param string $format 返りデータの日付フォーマット(省略可、秒単位まで指定可) * @return array 分割日付リスト */ function splitByMonthRange($first_date,$end_date,$month_range,$format='Y-m-d'){ $start = new DateTime($first_date); $end = new DateTime($end_date); $interval = DateInterval::createFromDateString($month_range.' month'); $period = new DatePeriod($start,$interval,$end); $dates = array(); foreach($period as $d){ $dates[] = $d->format($format); } return $dates; }
出力
array(30) { [0]=> string(19) "2015-01-01 00:00:00" [1]=> string(19) "2015-05-01 00:00:00" [2]=> string(19) "2015-09-01 00:00:00" [3]=> string(19) "2016-01-01 00:00:00" [4]=> string(19) "2016-05-01 00:00:00" [5]=> string(19) "2016-09-01 00:00:00" [6]=> string(19) "2017-01-01 00:00:00" [7]=> string(19) "2017-05-01 00:00:00" [8]=> string(19) "2017-09-01 00:00:00" [9]=> string(19) "2018-01-01 00:00:00" [10]=> string(19) "2018-05-01 00:00:00" [11]=> string(19) "2018-09-01 00:00:00" [12]=> string(19) "2019-01-01 00:00:00" [13]=> string(19) "2019-05-01 00:00:00" [14]=> string(19) "2019-09-01 00:00:00" [15]=> string(19) "2020-01-01 00:00:00" [16]=> string(19) "2020-05-01 00:00:00" [17]=> string(19) "2020-09-01 00:00:00" [18]=> string(19) "2021-01-01 00:00:00" [19]=> string(19) "2021-05-01 00:00:00" [20]=> string(19) "2021-09-01 00:00:00" [21]=> string(19) "2022-01-01 00:00:00" [22]=> string(19) "2022-05-01 00:00:00" [23]=> string(19) "2022-09-01 00:00:00" [24]=> string(19) "2023-01-01 00:00:00" [25]=> string(19) "2023-05-01 00:00:00" [26]=> string(19) "2023-09-01 00:00:00" [27]=> string(19) "2024-01-01 00:00:00" [28]=> string(19) "2024-05-01 00:00:00" [29]=> string(19) "2024-09-01 00:00:00" } ホーム