Negative DateInterval

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

    This took a little digging. The only way I was able to get a negative DateInterval was by doing this:

    $interval = DateInterval::createFromDateString('-1 day');
    

    However, there is a catch. DatePeriod seems to not work for negative intervals. If you set the start date to be before the end date then it doesn't contain any dates at all and if you flip so that the start date is after the end date then it looks backwards indefinitely.

    You're probably going to have to restructure your code to loop through the dates using DateTime::sub with a positive DateInterval or DateTime::add with the negative one.

提交回复
热议问题