What is InteractsWithTime?
Illuminate\Support\InteractsWithTime is a trait that centralises time and delay calculations. Over 40 classes in the Laravel framework use it — including every cache store, the rate limiter, queue console commands, the database connection, and console task components.
Source:
src/Illuminate/Support/InteractsWithTime.php. All methods are protected, so they are intended to be called from within the class that uses the trait.Where it is used in the framework
Method reference
secondsUntil() — remaining seconds
Returns the number of seconds until the given delay. Accepts a DateTimeInterface, a DateInterval, or an integer (seconds). Returns 0 for past dates.
availableAt() — available-at UNIX timestamp
Returns the UNIX timestamp when the delay will have elapsed. Used by caches for expiry times and by queues for delayed dispatch.
parseDateInterval() — normalise a delay value
Converts a DateInterval into a Carbon instance (current time + interval). Returns DateTimeInterface and integer values unchanged.
secondsUntil() and availableAt() call this method internally.
currentTime() — current UNIX timestamp
A thin wrapper around Carbon::now()->getTimestamp(). Used as the reference point inside secondsUntil().
runTimeForHumans() — human-readable elapsed time
Formats the difference between two microtime(true) values as a readable string. Under 1 000 ms it returns "42.15ms"; at 1 000 ms and above it uses CarbonInterval’s short forHumans() format ("1s 234ms").
Using the trait in packages
Accepting flexible TTL types
Cache-like classes often acceptint, DateInterval, or DateTime for TTL. InteractsWithTime handles all three:
Displaying command runtime
The testing variant
Illuminate\Foundation\Testing\Concerns\InteractsWithTime is a different trait — included automatically in TestCase — that provides time-travel helpers for tests:
Illuminate\Support\InteractsWithTime. The namespaces are different.
Summary
Add
use InteractsWithTime; to any class that needs to handle int, DateInterval, and DateTimeInterface delay values interchangeably, without writing the conversion logic yourself.