【问题标题】:PHP getting month dates and placing into arrayPHP获取月份日期并放入数组
【发布时间】:2013-02-09 22:16:29
【问题描述】:

今天我发现了这个函数,它显示一个月中有多少天 cal_days_in_month()。但我想要做的是将所有的日子都列在数组中,就像这样。

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

有人能指出我正确的方向吗?

亲切的问候 弗兰克!

【问题讨论】:

    标签: php arrays date datetime days


    【解决方案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

    参考

    【讨论】:

      【解决方案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);
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-16
        • 2018-12-14
        • 2018-01-30
        • 1970-01-01
        • 2011-11-03
        相关资源
        最近更新 更多