How to get all months between two dates in PHP?

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

    Just try with:

    $date1  = '2013-11-15';
    $date2  = '2014-02-15';
    $output = [];
    $time   = strtotime($date1);
    $last   = date('m-Y', strtotime($date2));
    
    do {
        $month = date('m-Y', $time);
        $total = date('t', $time);
    
        $output[] = [
            'month' => $month,
            'total' => $total,
        ];
    
        $time = strtotime('+1 month', $time);
    } while ($month != $last);
    
    
    var_dump($output);
    

    Output:

    array (size=4)
      0 => 
        array (size=2)
          'month' => string '11-2013' (length=7)
          'total' => string '30' (length=2)
      1 => 
        array (size=2)
          'month' => string '12-2013' (length=7)
          'total' => string '31' (length=2)
      2 => 
        array (size=2)
          'month' => string '01-2014' (length=7)
          'total' => string '31' (length=2)
      3 => 
        array (size=2)
          'month' => string '02-2014' (length=7)
          'total' => string '28' (length=2)
    

提交回复
热议问题