Carbon: diffInSeconds broken after PHP 7.2.12 release

Hello,

I encountered an issue with the following code:

$now = Carbon::now();

// some milliseconds pass.

echo Carbon::now()->diffInSeconds($now, false);

Carbon version: 1.34.1 PHP version: 7.2.12

I expected to get:

0

But I actually get:

A much higher number (differs depending on how much milliseconds have passed).

I believe this is because https://bugs.php.net/bug.php?id=77007 was merged for 7.2.12 causing differences between these versions. I’m not sure how this should be solved.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 24 (8 by maintainers)

Commits related to this issue

Most upvoted comments

https://bugs.php.net/bug.php?id=77007 is a very big problem for us and the way we handle difference. It clearly can be the root cause.

I’m a bit exhausted of the breaking patches of the PHP DateTime API. I will try to create a work-around.

You may be interested in the following possibilities:

  • You can set precision to 6 in MySQL to save datetime with microseconds, so you will no longer lost microseconds precision on database save/restore.
  • You can use ->startOfSecond() before diff calculation to force a second-rounding.

More precise support done in 1.35.0 and 2.5.3. Now to detect faster those breaking patches, I opened an other issue: https://github.com/briannesbitt/Carbon/issues/1511

Opened new ticket on PHP bug tracker: https://bugs.php.net/bug.php?id=77145

You’re welcome, we really appreciate Laravel too ! And I enjoy using it for a lot of my personal projects.