PHP 8.6 Introduces Standard SortDirection Enum and Time\Duration Class
PHP 8.6 adds two new standard library types, SortDirection and Time\Duration, to help unify how the language handles sorting and time spans.
PHP 8.6 introduces two standard-library types: the SortDirection enum and the Time\Duration class. SortDirection contains two cases, Ascending and Descending, and no other behavior. A GitHub search found over 150 separate, incompatible versions of this type.
Time\Duration represents a time span from one nanosecond to more than a hundred years with nanosecond precision. It provides named constructors like Time\Duration::fromMilliseconds(250) and exposes values via ->seconds, ->nanoseconds, and a ->negative flag. The Streams Project polling API accepts it in PHP 8.6.
PHP expresses time in at least six ways across functions like sleep(), CURLOPT_TIMEOUT_MS, usleep(), hrtime(), stream_set_timeout(), and microtime(true). Sorting direction also uses scattered integer constants, separate functions, userland strings, and booleans.
Tim Düsterhus and ext/date maintainer Derick Rethans had about two months from proposal to feature freeze to complete the work, with Tideways sponsoring part of Düsterhus's time. The Time\Duration RFC kept a narrow scope to avoid large APIs that remain nuisances for decades.
The types use new namespaces, following a pattern set by Random\* in PHP 8.2, Dom\* in PHP 8.4, and Uri\* in PHP 8.5.
A future RFC covering Time\Instant and a Time\Clock interface is under discussion on PHP Internals.
