Negative DateInterval

Viewed 30302

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

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

$this->Set('dates', new DatePeriod(new DateTime(), new DateInterval('P1Y'), new DateTime('2016-06-06')));

I want to start at 2016, and using a negative DateInterval move towards todays year

Something like this might illustrate my desire

$this->Set('dates', new DatePeriod(new DateTime('2016-06-06'), new DateInterval('-P1Y'), new DateTime()));

I just can't find any extended info on either DatePeriod or DateInterval on how to do this. All i find is that DateInterval can be inverted.

8 Answers

You can create negative DateInterval directly through constructor by passing strings in ISO 8601 format - ie:

>>> new \DateInterval("2009-03-01T00:00:00Z/2008-05-11T00:00:00Z")
=> DateInterval {#3947
     interval: - 9m 21d,
   }
Related