Hello,
I encountered an issue with the following code:
$requestTime = new \Carbon\Carbon('2018-11-14 18:23:12.0 +00:00');
$serverTime = new \Carbon\Carbon('2018-11-14 18:23:12.307628 +00:00');
return $serverTime->diffInSeconds($requestTime); //should be int 0 or int 1, but actually returns int 86399
Carbon version: 1.35.0
PHP version:7.2.12-1+ubuntu18.04.1+deb.sury.org+1
I expected to get:
But I actually get:
Obviously this is very close to being 1 day worth of seconds, so something strange appears to be going on somewhere...
Thanks!
Further to the above if you do the diff in the other direction, it works as expected...
echo $requestTime->diffInSeconds($serverTime, true) // 0 <-- correct
echo $serverTime->diffInSeconds($requestTime, true); // 86399 <-- incorrect
To give some more context, the $requestTime is created out of an RFC-7231 formatted timestamp which doesnt have microseconds. The $serverTime is created with a simple Carbon::now(new \DateTimeZone('GMT');) so obviously does have microseconds.
This bug only happens when the time being diffed doesn't have microseconds, but the time doing the diffing does have microseconds.
The above is on a Homestead Vagrant VirtualBox VM.
Running the same code on my local machine with PHP 7.2.11-4+ubuntu18.04.1+deb.sury.org+1, I can't reproduce the bug.
So it could be specific to PHP 7.2.12-1
Having the same issue, specifically on 7.2.12, not happening on 7.2.10
Also not happening on 7.2.11.
Yes, PHP has a known bug appearing in certains specific versions (PHP create an interval with year = -1, month = 11, days = 29). I thought we found a way, but seems not in all cases. A work-around while we figure it out is to trim microseconds:
$requestTime->startOfSecond()->diffInSeconds($serverTime->startOfSecond(), true)
http://php.net/ChangeLog-7.php#7.2.12
Date:
- Upgraded timelib to 2017.08.
- Fixed bug #75851 (Year component overflow with date formats "c", "o", "r" and "y").
- Fixed bug #77007 (fractions in
diff()are not correctly normalized).
Or simplest work-around (often more relevant too), use diffInRealSeconds that uses internally timestamps rather than date intervals. (So no call to diff() which has changed on PHP 7.2.12)
This should fix this case: #1517 waiting for tests to pass in every PHP versions to be sure.
Hi guys, please retry with 1.35.1.
Note: diff() results still uses native PHP DateTime with no filtering, so this fix catch the PHP bug introduced in PHP 7.1.12 in diffAsCarbonInterval and diffInXxx methods but raw methods from DateTime class will still produce unexpected outputs for differences less than 1 second.
Yep 1.35.1 fixes it :) Awesome stuff thanks so much for the rapid turnaround !!
$ artisan tinker
Psy Shell v0.9.9 (PHP 7.2.12-1+ubuntu18.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> $requestTime = new \Carbon\Carbon('2018-11-14 18:23:12.0 +00:00');
=> Carbon\Carbon @1542219792 {#2980
date: 2018-11-14 18:23:12.0 +00:00,
}
>>> $serverTime = new \Carbon\Carbon('2018-11-14 18:23:12.307628 +00:00');
=> Carbon\Carbon @1542219792 {#2981
date: 2018-11-14 18:23:12.307628 +00:00,
}
>>> $serverTime->diffInSeconds($requestTime);
=> 0
>>> $requestTime->diffInSeconds($serverTime);
=> 0
Thanks for the fix, works great 👍
Pulkit Jalan
On 15 Nov 2018, at 14:43, uphlewis notifications@github.com wrote:
Closed #1515.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
Most helpful comment
Hi guys, please retry with 1.35.1.
Note:
diff()results still uses native PHP DateTime with no filtering, so this fix catch the PHP bug introduced in PHP 7.1.12 in diffAsCarbonInterval and diffInXxx methods but raw methods from DateTime class will still produce unexpected outputs for differences less than 1 second.