DateInterval::__construct

Description

publicDateInterval::__construct
( string$interval_spec
)

Creates a new DateInterval object.

Parameters

interval_spec

An interval specification.

The format starts with the letter P,
for period.
Each duration period is represented by an integer value
followed by a period designator.
If the duration contains time elements, that portion
of the specification is preceded by the letter
T.

interval_spec Period Designators

Period Designator

Description

Y

years

M

months

D

days

W

weeks. These get converted into days,
so can not be combined with D.

H

hours

M

minutes

S

seconds

Here are some simple examples.
Two days is P2D.
Two seconds is PT2S.
Six years and five minutes is P6YT5M.

Note:

The unit types must be entered from the largest
scale unit on the left to the smallest scale unit
on the right.
So years before months, months before days,
days before minutes, etc.
Thus one year and four days must be represented as
P1Y4D, not P4D1Y.

The specification can also be represented as a date time.
A sample of one year and four days would be
P0001-00-04T00:00:00.
But the values in this format can not exceed a given period's
roll-over-point (e.g. 25 hours is invalid).

Note that, while a DateInterval object has an $invert property, you cannot supply a negative directly to the constructor similar to specifying a negative in XSD ("-P1Y"). You will get an exception through if you do this.

Instead you need to construct using a positive interval ("P1Y") and the specify the $invert property === 1.

Although PHP refers to periods of time as "intervals", ISO 8601 refers to them as "durations". In ISO 8601, "intervals" are something else.

While ISO 8601 allows fractions for all parts of a duration (e.g., "P0.5Y"), DateInterval does not. Use caution when calculating durations. If the duration has a fractional part, it may be lost when storing it in a DateInterval object.