How to get all months between two dates in PHP?

前端 未结 6 1888
旧巷少年郎
旧巷少年郎 2021-01-29 16:41

I have 2 dates. I want to get all months with total days in each.

How can I do this in PHP?

For example

$date1 = \'2013-11-13\'         


        
6条回答
  •  没有蜡笔的小新
    2021-01-29 17:11

    Try the below given code :

              $date1 = '2013-11-15'; // yy-mm-dd format
              $date2 = '2014-02-15';
              $start    = new DateTime($date1);
    
              $start->modify('first day of this month');
              $end      = new DateTime($date2);
              $end->modify('first day of next month');
              $interval = DateInterval::createFromDateString('1 month');
              $period   = new DatePeriod($start, $interval, $end);
    
              foreach ($period as $dt) {
                     echo $dt->format("Y-m") ."  " ;
                     echo cal_days_in_month(CAL_GREGORIAN,$dt->format("m"),$dt->format("Y")) . "
    "; }

    Also, checkout this link for more

提交回复
热议问题