How to get all months between two dates in PHP?

前端 未结 6 1823
旧巷少年郎
旧巷少年郎 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")) . "<br/>";
              }
    

    Also, checkout this link for more

    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-01-29 17:15

    You can use the DateTime class along with cal_day_in_month() like this

    $datetime1 = "2014-02-15";
    $datetime2= "2013-03-15";
    
    $date1 = new DateTime($datetime1);
    $date2 = new DateTime($datetime2);
    
    while (date_format($date2, 'Y-m') <= date_format($date1, 'Y-m'))
    {
    
        $date2 = $date2->add(new DateInterval('P1M'));
    
        echo $date2->format('Y-m')." | ".cal_days_in_month(CAL_GREGORIAN, $date2->format('m'), $date2->format('Y'))."<br>";
    
    }
    
    0 讨论(0)
  • 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"))."<br>\n";    
    }
    
    0 讨论(0)
  • 2021-01-29 17:24

    This is what i came up with:

    $arr_months = array();
    $date1 = new DateTime('2013-11-15');
    $date2 = new DateTime('2014-02-15');
    $month1 = new DateTime($date1->format('Y-m')); //The first day of the month of date 1
    while ($month1 < $date2) { //Check if the first day of the next month is still before date 2
        $arr_months[$month1->format('Y-m')] = cal_days_in_month(CAL_GREGORIAN, $month1->format('m'), $month1->format('Y')); //Add it to the array
        $month1->modify('+1 month'); //Add one month and repeat
        }
    
    print_r($arr_months);
    

    It creates an associative array with the month as key and the number of days as value. The array created from the example would be:

    Array
    (
        [2013-11] => 30
        [2013-12] => 31
        [2014-01] => 31
        [2014-02] => 28
    )
    

    With a foreach loop you will be able to scroll trough the array easily.

    0 讨论(0)
  • 2021-01-29 17:31

    i used timestamp and date:

    $date1 = '2013-11-15'; // yy-mm-dd format
    $date2 = '2014-02-15';
    
    $d1 = strtotime('2013-11-15');
    $d2 = strtotime('2014-02-15');
    
    while ($d1 <= $d2) {
        echo date('m-d-Y', $d1)." | ";
        echo cal_days_in_month(CAL_GREGORIAN, date('m', $d1), date('Y', $d1)) ."<br>";
        $d1 = strtotime("+1 month", $d1);
    
    }
    
    0 讨论(0)
提交回复
热议问题