How to get all months between two dates in PHP?

前端 未结 6 1824
旧巷少年郎
旧巷少年郎 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:16

    This should help you, Check out,

    $date1 = new DateTime('2013-11-15'); 
    $date1->modify('first day of this month');
    $date2 = new DateTime('2014-02-15');
    $date2->modify('first day of next month');
    $interval = DateInterval::createFromDateString('1 month');
    $value  = new DatePeriod($date1, $interval, $date2);
    
    
    foreach ($value as $dates) {
      echo $dates->format("m- Y")."-->".cal_days_in_month(0,$dates->format("m"),$dates->format("Y"))."
    \n"; }

提交回复
热议问题