2013-02-09 41 views
0

今天我发现这个函数说明一个月中有多少天是cal_days_in_month()。但是我想要做的是将所有日子列入数组中,像这样。PHP获取月份日期并放入数组

$days = array(
"2012-11-01","2012-12-01","2012-13-01"...etc 
) 

难道有人指着我走向正确的方向吗?

亲切的问候 弗兰克!

回答

2
$start = new DateTime('first day of this month'); 
$end  = new DateTime('first day of next month'); 
$interval = DateInterval::createFromDateString('1 day'); 
$period = new DatePeriod($start, $interval, $end); 

foreach ($period as $dt) 
{ 
    echo $dt->format("l Y-m-d") . PHP_EOL; 
} 

See it in action

参考

+1

+1'DateTime' – philwinkle 2013-02-09 22:23:01

0
$fromdate=strtotime(date("Y-m-d",mktime(0, 0, 0, $month, '01', $year))); 
    $todate=strtotime(date("Y-m-t",mktime(0, 0, 0, $month, '01', $year))); 

     for($i=$fromdate;$i<=$todate;$i++) 
     { 
     $i=$i+84600; 
     echo  $nextdate[]=date('Y-m-d',$i); 
     }