[PHP] 対象月1ヶ月の日付を配列で取得する方法
 月次の集計システムなどを作る時に便利に使える、1ヶ月分の日付を取得するスニペットです。
月次の集計システムなどを作る時に便利に使える、1ヶ月分の日付を取得するスニペットです。
function get_dates($year=null, $month=null){
  $y  = $year;
  $m  = sprintf("%02d", (int)$month);
  $first_day = "{$y}-{$m}";
  $dates = [];
  for($i=(int)date('d', strtotime($first_day)); $i<=(int)date('d', strtotime("{$first_day} last day of this month")); $i++){
    $d = sprintf("%02d", $i);
    $dates[] = "{$y}-{$m}-{$d}";
  }
  return $dates;
}