Negative DateInterval

前端 未结 7 2316
后悔当初
后悔当初 2020-12-17 09:59

I want to create a DatePeriod object with a negative DateInterval.

This creates a DatePeriod with the year increasing from today to 2016.

$this->S         


        
7条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-17 10:12

    EDIT: note that this was inspired by khany's code above.

    Here's a fully working script for my use-case, which is to display year+month strings from the current month going back N number of months. It should work with days or years as intervals and is tested with PHP version 5.3.3.

    format('Ym'));
        $previousMonthDate->sub(new DateInterval("P1M"));
    }
    
    print_r($monthDateList) . "\n";
    
    ?>
    

    The output is:

    Array
    (
        [0] => 201705
        [1] => 201704
        [2] => 201703
        [3] => 201702
        [4] => 201701
        [5] => 201612
        [6] => 201611
        [7] => 201610
        [8] => 201609
        [9] => 201608
        [10] => 201607
        [11] => 201606
        [12] => 201605
        [13] => 201604
        [14] => 201603
        [15] => 201602
    )
    

提交回复
热议问题