Skip to content

Conversation

@SanderMuller
Copy link
Contributor

This PR updates the duration helpers (microseconds, milliseconds, seconds, minutes, hours, days) to accept int|float values.

CarbonInterval natively supports fractional values for all of these units (as reflected in its docblock for the corresponding add{Unit} methods), and the conversion is internally handled through microseconds. Updating the helpers aligns Laravel’s type signatures with Carbon’s documented behavior.

Why?

Developers often work with derived or calculated durations, for example:

milliseconds($expectedSeconds * 1000)

Without float support, this requires extra casting:

milliseconds((int) ($expectedSeconds * 1000))

Allowing floats removes unnecessary boilerplate and matches the actual capabilities of CarbonInterval.

Benefits

  • Better alignment with CarbonInterval’s documented API
  • Cleaner, more natural usage when dealing with computed durations
  • No breaking changes: existing code continues to work, and Carbon gracefully handles floats

@shaedrich
Copy link
Contributor

While I agree that it should support what Carbon supports, your example is quite contrived. Instead of

milliseconds($expectedSeconds * 1000)

would just do

seconds($expectedSeconds)

@SanderMuller
Copy link
Contributor Author

While I agree that it should support what Carbon supports, your example is quite contrived. Instead of

milliseconds($expectedSeconds * 1000)

would just do

seconds($expectedSeconds)

Agreed, although they are only completely the same after calling ->cascade(), until then they are stored differently internally. I remember that milliseconds($expectedSeconds * 1000) over seconds($expectedSeconds) solved a bug somewhere in our codebase. Might be that the actual bug was an odd call on the CarbonInterval instance, though.

Either way, this PR will indeed add support for seconds(1.4) to result in 1400ms

@taylorotwell taylorotwell merged commit 6567d33 into laravel:12.x Dec 4, 2025
75 of 76 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants