Download the PHP package componenta/duration without Composer
On this page you can find all versions of the php package componenta/duration. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download componenta/duration
More information about componenta/duration
Files in componenta/duration
Package duration
Short Description Immutable ISO 8601 duration value object
License MIT
Informations about the package duration
Componenta Duration
Immutable ISO 8601 duration value object with calendar-aware and fixed-duration operations.
Use it when application code needs a typed duration instead of passing raw seconds or raw ISO strings.
Installation
Related Packages
This package is standalone.
| Package | Why it may be used nearby |
|---|---|
componenta/clock |
Provides reference dates for calendar-aware comparisons. |
componenta/config |
Can store ISO 8601 durations that are converted to Duration. |
componenta/validation |
Can validate user strings before creating the value object. |
Usage
Factories are available for common units:
Calendar Components
Years and months are calendar components. They are not converted to seconds without a reference date because their exact length depends on the calendar.
Weeks are expanded to days when parsing ISO 8601 because the value object stores days, not a separate week component.
Arithmetic
Duration supports immutable arithmetic:
addsubtractsubtractClampedmultiplydivideBysumminmax
Use compareTo() only for comparable durations. Calendar-aware comparison is available through compareToFrom() with a reference date.
Conversion
The object can convert to:
- ISO 8601 string
DateInterval- dense array through
toArray() - sparse array through
toSparseArray() - custom string through
format() - JSON through
jsonSerialize()
applyTo() adds the duration to a DateTimeInterface and returns a DateTimeImmutable.
Supported Format
The parser supports integer ISO 8601 components: P[n]Y[n]M[n]W[n]DT[n]H[n]M[n]S.
Fractional components such as PT1.5S are rejected.