【发布时间】:2012-11-01 00:50:22
【问题描述】:
我正在尝试但仍然想知道,如何获得一个包含当月所有日期的数组,它应该包含格式为:年-月-日的所有日期。感谢您的帮助!
【问题讨论】:
我正在尝试但仍然想知道,如何获得一个包含当月所有日期的数组,它应该包含格式为:年-月-日的所有日期。感谢您的帮助!
【问题讨论】:
这个怎么样:
$list=array();
for($d=1; $d<=31; $d++)
{
$time=mktime(12, 0, 0, date('m'), $d, date('Y'));
if (date('m', $time)==date('m'))
$list[]=date('Y-m-d', $time);
}
var_dump($list);
【讨论】:
试试:
// 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);
【讨论】:
返回此类数组的简单函数如下所示:
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;
}
如果需要,您可以通过将步骤 (+1 day) 和输出格式 (Y-m-d) 更改为可选参数来改进它。
【讨论】: