Iso8601Duration
Represents and manipulates ISO 8601 duration strings.
This class provides a convenient wrapper around PHP's DateInterval with additional functionality for working with ISO 8601 duration format. It maintains synchronization between the string representation and the DateInterval object.
ISO 8601 duration format: P[n]Y[n]M[n]DT[n]H[n]M[n]S
- P: duration designator (required)
- T: time designator (separates date and time components)
- Y/M/D: years, months, days
- H/M/S: hours, minutes, seconds (after T)
Tags
Table of Contents
Constants
- DAY : string = 'D'
- Day designator.
- HOUR : string = 'H'
- Hour designator.
- MINUTE : string = 'M'
- Minute designator (used in time component after T).
- MONTH : string = 'M'
- Month designator (used in date component before T).
- PATTERN : string = '/^P(?:(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(?:T(\d+H)?(...
- ISO 8601 duration regex pattern : P[n]Y[n]M[n]W[n]DT[n]H[n]M[n]S or P[n]W
- PERIOD : string = 'P'
- Duration designator (required at the start of every duration string).
- SECOND : string = 'S'
- Second designator.
- TIME : string = 'T'
- Time designator (separates date and time components).
- WEEK : string = 'W'
- Week designator (alternative to day specification).
- YEAR : string = 'Y'
- Year designator.
- ZERO : string = 'P0D'
- Zero duration constant.
Properties
- $days : int
- Gets the number of days in the duration.
- $hours : int
- Gets the number of hours in the duration.
- $interval : DateInterval
- Gets or sets the DateInterval representation of the duration.
- $iso : string
- Gets or sets the ISO 8601 string representation of the duration.
- $minutes : int
- Gets the number of minutes in the duration.
- $months : int
- Gets the number of months in the duration.
- $seconds : int
- Gets the number of seconds in the duration.
- $years : int
- Gets the number of years in the duration.
- $_interval : DateInterval
- The internal DateInterval representing the duration.
- $_iso : string
- The ISO 8601 string representation of the duration.
Methods
- __construct() : mixed
- Creates a new Iso8601Duration instance from an ISO 8601 string or a DateInterval.
- __toString() : string
- Returns the string representation of the duration.
- addTo() : DateTime
- Adds this duration to a given DateTime and returns a new DateTime object.
- subtractFrom() : DateTime
- Subtracts this duration from a given DateTime and returns a new DateTime object.
- toSeconds() : int
- Converts the duration to an approximate number of seconds.
Constants
DAY
Day designator.
public
string
DAY
= 'D'
HOUR
Hour designator.
public
string
HOUR
= 'H'
MINUTE
Minute designator (used in time component after T).
public
string
MINUTE
= 'M'
MONTH
Month designator (used in date component before T).
public
string
MONTH
= 'M'
PATTERN
ISO 8601 duration regex pattern : P[n]Y[n]M[n]W[n]DT[n]H[n]M[n]S or P[n]W
public
string
PATTERN
= '/^P(?:(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(?:T(\d+H)?(\d+M)?(\d+(?:\.\d+)?S)?)?|0[YMWD]|T0S)$/'
PERIOD
Duration designator (required at the start of every duration string).
public
string
PERIOD
= 'P'
SECOND
Second designator.
public
string
SECOND
= 'S'
TIME
Time designator (separates date and time components).
public
string
TIME
= 'T'
WEEK
Week designator (alternative to day specification).
public
string
WEEK
= 'W'
YEAR
Year designator.
public
string
YEAR
= 'Y'
ZERO
Zero duration constant.
public
string
ZERO
= 'P0D'
Properties
$days read-only virtual
Gets the number of days in the duration.
public
int
$days
Tags
Hooks
public
int
get
$hours read-only virtual
Gets the number of hours in the duration.
public
int
$hours
Tags
Hooks
public
int
get
$interval virtual
Gets or sets the DateInterval representation of the duration.
public
DateInterval
$interval
When setting this property, the internal DateInterval is cloned to prevent external modifications, and the ISO 8601 string is automatically regenerated.
Tags
Hooks
public
DateInterval
get
public
set
$iso virtual
Gets or sets the ISO 8601 string representation of the duration.
public
string
$iso
When setting this property, the DateInterval is automatically updated and validated. Invalid ISO 8601 strings will throw an exception.
Tags
Hooks
public
string
get
public
set
$minutes read-only virtual
Gets the number of minutes in the duration.
public
int
$minutes
Tags
Hooks
public
int
get
$months read-only virtual
Gets the number of months in the duration.
public
int
$months
Tags
Hooks
public
int
get
$seconds read-only virtual
Gets the number of seconds in the duration.
public
int
$seconds
Tags
Hooks
public
int
get
$years read-only virtual
Gets the number of years in the duration.
public
int
$years
Tags
Hooks
public
int
get
$_interval
The internal DateInterval representing the duration.
private
DateInterval
$_interval
$_iso
The ISO 8601 string representation of the duration.
private
string
$_iso
Methods
__construct()
Creates a new Iso8601Duration instance from an ISO 8601 string or a DateInterval.
public
__construct([string|DateInterval|null $duration = null ]) : mixed
Parameters
- $duration : string|DateInterval|null = null
-
ISO 8601 duration string (e.g., "P1Y2M", "PT4H30M", "P1W"), a DateInterval object, or null for zero duration (P0D)
Tags
__toString()
Returns the string representation of the duration.
public
__toString() : string
This magic method allows the object to be used in string contexts, returning the ISO 8601 duration string.
Tags
Return values
string —The ISO 8601 duration string
addTo()
Adds this duration to a given DateTime and returns a new DateTime object.
public
addTo(DateTime $date) : DateTime
The original DateTime is not modified; a clone is created and modified instead. This method properly handles calendar arithmetic including leap years, varying month lengths, and daylight saving time transitions.
Parameters
- $date : DateTime
-
The reference date to add the duration to
Tags
Return values
DateTime —A new DateTime object with the duration added
subtractFrom()
Subtracts this duration from a given DateTime and returns a new DateTime object.
public
subtractFrom(DateTime $date) : DateTime
The original DateTime is not modified; a clone is created and modified instead. This method properly handles calendar arithmetic including leap years, varying month lengths, and daylight saving time transitions.
Parameters
- $date : DateTime
-
The reference date to subtract the duration from
Tags
Return values
DateTime —A new DateTime object with the duration subtracted
toSeconds()
Converts the duration to an approximate number of seconds.
public
toSeconds() : int
This method provides an estimation by converting:
- 1 year = 365 days
- 1 month = 30 days
- 1 day = 86400 seconds
For exact calculations that account for actual calendar variations (leap years, different month lengths), use addTo() with a specific reference date instead.
Tags
Return values
int —The approximate number of seconds in the duration