Download the PHP package thesis/time-span without Composer
On this page you can find all versions of the php package thesis/time-span. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package time-span
Thesis TimeSpan
An immutable, nanosecond-precise time duration type for PHP 8.3+.
Installation
Why not DateInterval?
PHP's built-in DateInterval covers calendar durations — periods like "3 months" or "1 year" that only make sense
relative to a specific date. TimeSpan covers the complementary case: a fixed amount of elapsed time that exists
independently of any calendar.
DateInterval |
TimeSpan |
|
|---|---|---|
| Represents | calendar period | fixed duration |
| Supports months/years | yes | no |
| Immutable | no | yes |
| Arithmetic | no | add sub mul div |
| Comparison | no | compareTo isLessThan … |
| Negative values | awkward invert flag |
signed int |
| Precision | microseconds | nanoseconds |
DateInterval is the right tool when you need "30 days from now" or "next month". TimeSpan is the right tool when you
need "wait 30 seconds" or "this request took 42 ms".
Use cases
Creating a TimeSpan
From multiple units
From a single unit
All constructors accept int|float. Floats are rounded to the nearest nanosecond.
From a DateInterval
Note: Intervals with years or months cannot be converted to a fixed duration and will throw an
InvalidArgumentException. Intervals produced byDateTimeInterface::diff()are also rejected due to DST ambiguity — useTimeSpan::between()instead.
Between two datetimes
The result is signed: between($a, $b) returns a negative span if $b is in the past relative to $a.
From the high-resolution timer
Directly from nanoseconds
Converting to other units
Every to*() method returns int by default. Pass a $precision argument to get a float with that many decimal
places.
The optional second argument controls rounding mode (defaults to PHP_ROUND_HALF_UP):
Arithmetic
All arithmetic methods return a new TimeSpan instance and throw \OverflowException
if the result exceeds the int range (~292 years in nanoseconds on 64-bit platform).
div() throws \DivisionByZeroError when the factor is 0.
Comparison
Sign checks
Formatting
format()
format() renders a span as a human-readable string. The default pattern is %-%h:%i:%s.
| Placeholder | Unit | Width |
|---|---|---|
%- |
sign | 0–1 |
%d |
days | >=1 (unpadded) |
%h |
hours | >=2 (zero-padded) |
%i |
minutes | >=2 (zero-padded) |
%s |
seconds | >=2 (zero-padded) |
%ms |
milliseconds | >=3 (zero-padded) |
%us |
microseconds | >=3 (zero-padded) |
%ns |
nanoseconds | >=3 (zero-padded) |
The largest unit present in the format receives the total cumulative value; each smaller unit shows only the remainder after the larger ones are subtracted:
The sign is only included when %- is explicitly present in the format:
__toString()
__toString() is equivalent to format() with the default pattern:
License
MIT