Uh oh!
There was an error while loading. Please reload this page.
[4.6] fix: Time::createFromTimestamp() change for PHP 8.4 - #9105
Conversation
4aa7a39 to
3b51c3eCompare| $timestamp = 1489762800.654321; | ||
| // The timezone will be UTC if you don't specify. | ||
| $time = Time::createFromTimestamp($timestamp); | ||
| // float cannot handle the number precisely. | ||
| $this->assertSame('2017-03-17 15:00:00.654300', $time->format('Y-m-d H:i:s.u')); |
There was a problem hiding this comment.
I thought this was due to float, but the following test passes. Why?
$timestamp = 1489762800.654321;
$time = \DateTimeImmutable::createFromTimestamp($timestamp);
$this->assertSame('2017-03-17 15:00:00.654321', $time->format('Y-m-d H:i:s.u'));There was a problem hiding this comment.
The u format expresses the time in microseconds which is a millionth of a second. So, the expressed time is six decimal digits.
There was a problem hiding this comment.
I meant the following test passes. Why 654300 and 654321?
$timestamp = 1489762800.654321;
$time1 = Time::createFromTimestamp($timestamp);
$time2 = \DateTimeImmutable::createFromTimestamp($timestamp);
$this->assertSame('2017-03-17 15:00:00.654300', $time1->format('Y-m-d H:i:s.u'));
$this->assertSame('2017-03-17 15:00:00.654321', $time2->format('Y-m-d H:i:s.u'));There was a problem hiding this comment.
It probably has something to do with the default precision (which is 14 by default).
In the Time::createFromTimestamp method, we use:
$time = newstatic('@' . $timestamp, 'UTC', $locale);Which will "cut" the ending and make it: .6543. We can either set ini_set('precision', 16) at the framework level which is not ideal or use sprintf here:
$time = newstatic(sprintf('@%.6f', $timestamp), 'UTC', $locale);There was a problem hiding this comment.
sprintf('@%.6f', $timestamp) works. Thank you!
3ff50fb to
2b819acCompareIt returns Time with the default timezone as before.
It returns Time with the default timezone as before.
2b819ac to
97148efCompareThe microseconds should be preserved without rounding.
Needs #9107Description
Follow-up #8544
Time::createFromTimestamp()(without timezone specified) should returnTimewith the timezone UTC, becauseDateTime(Immutable)::createFromTimestamp()in PHP 8.4 returns an instance with the timezone+00:00.selfwithstatic.PHP 8.4 introduces new
DateTime(Immutable)::createFromTimestamp()method.https://php.watch/versions/8.4/datetime-createFromTimestamp
Carbon and Chronos have changed the behaviors.
Checklist: