Php数组包含当前月份的所有日期

Moo*_*ozy 9 php arrays date

我正在尝试并仍在疑惑,我如何获得一个包含当月所有日期的数组,它应该包含格式的所有日期:年 - 月 - 日.谢谢你的帮助!

Tee*_*mas 17

尝试:

// for each day in the month
for($i = 1; $i <=  date('t'); $i++)
{
   // add the date to the dates array
   $dates[] = date('Y') . "-" . date('m') . "-" . str_pad($i, 2, '0', STR_PAD_LEFT);
}

// show the dates array
var_dump($dates);
Run Code Online (Sandbox Code Playgroud)


oez*_*ezi 6

返回这样一个数组的简单函数可能如下所示:

function range_date($first, $last) {
  $arr = array();
  $now = strtotime($first);
  $last = strtotime($last);

  while($now <= $last ) {
    $arr[] = date('Y-m-d', $now);
    $now = strtotime('+1 day', $now);
  }

  return $arr;
}
Run Code Online (Sandbox Code Playgroud)

如果需要,您可以通过将步骤 ( +1 day) 和输出格式 ( Y-m-d)更改为可选参数来改进它。