Negative DateInterval

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

    According to comment by kevinpeno at 17-Mar-2011 07:47 on php.net's page about DateInterval::__construct(), you cannot directly create negative DateIntervals through the constructor:

    new DateInterval('-P1Y'); // Exception "Unknown or bad format (-P1Y)"
    

    Instead of this you are required to create a positive interval and explicitly set it's invert property to 1:

    $di = new DateInterval('P1Y');
    $di->invert = 1; // Proper negative date interval
    

    Just checked the above code by myself, it's working exactly in this way.

提交回复
热议问题