PHP getting month dates and placing into array

给你一囗甜甜゛ 提交于 2019-12-12 03:09:09

问题


Today I found this function that says how many days were in a month cal_days_in_month(). But what I am wanting to do is list all the days into array, something like this.

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

Could someone point me into the right direction?

Kind regards Frank!


回答1:


$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

Reference

  • DateTime
  • DatePeriod
  • DateInterval



回答2:


 $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);
        }


来源:https://stackoverflow.com/questions/14792217/php-getting-month-dates-and-placing-into-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!