Download the PHP package bermudaphp/duration without Composer
On this page you can find all versions of the php package bermudaphp/duration. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bermudaphp/duration
More information about bermudaphp/duration
Files in bermudaphp/duration
Package duration
Short Description An immutable PHP library for handling time durations with ISO 8601 support
License MIT
Informations about the package duration
Duration
Read this in other languages: Русский
Overview
Duration
is an immutable wrapper class for working with time durations with ISO 8601 support. It provides a comprehensive API for creating, manipulating, formatting, and comparing time durations in a type-safe and consistent manner.
Table of Contents
- Features
- Installation
- Basic Usage
- Factory Methods
- ISO 8601 Support
- Conversion Methods
- Arithmetic Operations
- Comparison Operations
- Increment and Decrement Operations
- Formatting Methods
- Serialization
- DateInterval Conversion
- Examples
- Testing
- Contributing
- License
Features
- Immutable Design: All operations return new instances, preserving the original
- Fluent Interface: Method chaining for cleaner code
- ISO 8601 Support: Parse and generate ISO 8601 duration strings (e.g.,
P1Y2M3DT4H5M6S
) - Comprehensive Time Units: Convert between seconds, minutes, hours, days, weeks, months, and years
- Advanced Comparison: Compare durations with support for ALL/ANY modes when working with arrays
- Increment/Decrement: Convenient methods for increasing or decreasing durations
- Multiple Formatting Options: Human-readable, custom format templates, and component arrays
- Serialization Support: JSON serialization built-in
- DateInterval Integration: Convert to and from PHP's DateInterval class
Installation
Basic Usage
Factory Methods
Duration
provides multiple factory methods for creating instances:
ISO 8601 Support
Duration
fully supports the ISO 8601 duration format:
ISO 8601 Format Rules
- Format:
P[n]Y[n]M[n]DT[n]H[n]M[n]S
P
is the duration designator (for period) placed at the startY
is the year designatorM
is the month designatorD
is the day designatorT
is the time designator (required if any time components are used)H
is the hour designatorM
is the minute designatorS
is the second designator
Example: P3Y6M4DT12H30M5S
represents a duration of 3 years, 6 months, 4 days, 12 hours, 30 minutes, and 5 seconds.
Conversion Methods
Convert a duration to different time units:
Arithmetic Operations
Perform arithmetic operations on durations:
Comparison Operations
Compare durations with various methods:
Comparing with Arrays of Durations
Compare a duration against multiple durations using ALL or ANY modes:
Increment and Decrement Operations
Increment or decrement durations:
Formatting Methods
Format durations in different ways:
Format Placeholders
The format()
method supports the following placeholders:
Placeholder | Description |
---|---|
%Y |
Years (zero-padded) |
%M |
Months (zero-padded) |
%D |
Days (zero-padded) |
%H |
Hours (zero-padded) |
%I |
Minutes (zero-padded) |
%S |
Seconds (zero-padded) |
%T |
Total seconds |
Serialization
The Duration
class implements JsonSerializable
and Stringable
interfaces:
DateInterval Conversion
Convert between Duration
and PHP's DateInterval
:
Examples
Creating a Countdown Timer
Working with Events
Tracking Task Time
Testing
Run the PHPUnit tests:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.