DateInterval::__construct

DateInterval::__construct - Creates a new DateInterval object
Manual
Code Examples

Description public DateInterval::__construct stringduration

Creates a new DateInterval object.

Parameters

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.

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).

These formats are based on the ISO 8601 duration specification.

Exceptions and Errors

Throws an Exception when the duration cannot be parsed as an interval.

Changelog

Version Description
8.0.0 W can be combined with D.

Related Functions

Example of DateInterval::__construct

Show all examples for DateInterval::__construct

PHP Version:


Function DateInterval::__construct:

Date and Time Functions

Most used PHP functions