It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened,
visit the help center.
Closed 7 years ago.
I'm trying and still wondering, how I can get an array which contains all of the dates in the current month, it should contain all of the dates in format: year-month-day. Thanks for your help!
Try:
// 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);
a simple function to return such an array could look like this:
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;
}
if needed, you can improve it by changing the step (+1 day
) and the output format (Y-m-d
) into optional parameters.
How about this:
$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);