Negative DateInterval

前端 未结 7 2250
后悔当初
后悔当初 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:19

    I tried it myself and it isn't possible with DatePeriod alone, but I think that makes sense: It just reflects the periods, that usually doesn't have any specific order and therefore cannot get reordered (it can be treated as a set).

    The only way to retrieve the dates and sort it in reversed order, as far as I can see, is something like this

    $result = array();
    forech ($dateperiod as $date) {
      array_push ($result, $data);
    }
    

    Update

    $date = new DateTime('2016-06-06');
    $i = new DateInterval('P1Y');
    $now = new DateTime;
    while ($date >= $now) {
      echo $date->format('c') . PHP_EOL;
      $date = $date->sub($i);
    }
    

提交回复
热议问题