class DateInterval (View source)

Representation of date interval. A date interval stores either a fixed amount of time (in years, months, days, hours etc) or a relative time string in the format that DateTime's constructor supports.

Properties

int $y

Number of years

int $m

Number of months

int $d

Number of days

int $h

Number of hours

int $i

Number of minutes

int $s

Number of seconds

float Since: 7.1.0
$f

Number of microseconds

int $invert

Is 1 if the interval is inverted and 0 otherwise

int|false $days

Total number of days the interval spans. If this is unknown, days will be FALSE.

Methods

__construct(string $duration)

Creates a new DateInterval object

string
format(string $format)

Formats the interval

static DateInterval|false
createFromDateString(string $datetime)

Sets up a DateInterval from the relative parts of the string

void
__wakeup()

No description

static DateInterval
__set_state(array $array)

No description

array
__serialize()

No description

void
__unserialize(array $data)

No description

Details

__construct(string $duration)

Creates a new DateInterval object

Parameters

string $duration

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. duration Period Designators Period Designator Description Y years M months D days W weeks. Converted into days. Prior to PHP 8.0.0, 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. 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). These formats are based on the ISO 8601 duration specification.

Exceptions

DateMalformedIntervalStringException

string format(string $format)

Formats the interval

Parameters

string $format

The following characters are recognized in the format parameter string. Each format character must be prefixed by a percent sign (%). format character Description Example values % Literal % % Y Years, numeric, at least 2 digits with leading 0 01, 03 y Years, numeric 1, 3 M Months, numeric, at least 2 digits with leading 0 01, 03, 12 m Months, numeric 1, 3, 12 D Days, numeric, at least 2 digits with leading 0 01, 03, 31 d Days, numeric 1, 3, 31 a Total number of days as a result of a DateTime::diff or (unknown) otherwise 4, 18, 8123 H Hours, numeric, at least 2 digits with leading 0 01, 03, 23 h Hours, numeric 1, 3, 23 I Minutes, numeric, at least 2 digits with leading 0 01, 03, 59 i Minutes, numeric 1, 3, 59 S Seconds, numeric, at least 2 digits with leading 0 01, 03, 57 s Seconds, numeric 1, 3, 57 F Microseconds, numeric, at least 6 digits with leading 0 007701, 052738, 428291 f Microseconds, numeric 7701, 52738, 428291 R Sign "-" when negative, "+" when positive -, + r Sign "-" when negative, empty when positive -,

Return Value

string

Returns the formatted interval.

static DateInterval|false createFromDateString(string $datetime)

Sets up a DateInterval from the relative parts of the string

Parameters

string $datetime

A date with relative parts. Specifically, the relative formats supported by the parser used for DateTimeImmutable, DateTime, and strtotime will be used to construct the DateInterval. To use an ISO-8601 format string like P7D, you must use the DateInterval::__construct.

Return Value

DateInterval|false

Returns a new https://www.php.net/manual/en/class.dateinterval.php DateInterval instance on success, or FALSE on failure.

Exceptions

DateMalformedIntervalStringException

void __wakeup()

No description

Return Value

void

static DateInterval __set_state(array $array)

No description

Parameters

array $array

Return Value

DateInterval

array __serialize()

No description

Return Value

array

void __unserialize(array $data)

No description

Parameters

array $data

Return Value

void