Repository navigation
formatDuration
Corey Avis edited this page Sep 23, 2026
·
2 revisions
Calculates the time difference between two dates and returns a human-readable duration string with automatic pluralization and configurable unit abbreviations.
formatDuration(string|integer $start = 0, string|integer $end = 0): string
start (string|integer)
: The starting point. Accepts date strings ("2026-01-01 10:00:00"), relative dates ("-2 days"), or Unix timestamps (1700000000). (default: 0)
end (string|integer)
: The end point. Accepts the same formats as $start. Defaults to current system date/time if omitted or set to 0. (default: 0) ^(optional)^
(string)
: Returns a space-separated list of active time components (e.g., "1 year 2 months", "45 minutes").
: Returns "0 seconds" (or "0 secs") if $start is missing, invalid, or identical to $end.
$fx->formatDuration('2024-01-01 00:00:00', '2026-03-05 04:15:30');
// Result: 2 years 2 months 4 days 4 hours 15 minutes 30 seconds
| Config Key | Default Value | Allowed Values | Description |
|---|---|---|---|
| abbr | false | bool | Abbreviates certain words like hours to hrs. |
- Triggers a warning if the start date is omitted.
- Triggers a warning if an invalid date format is provided.