Req #79692 [Opn->Csd]: Creating DateTime from unixtime should support fractional seconds
| From: | derick@php.net | Date: | Sat, 04 Jun 2022 17:22:38 +0000 |
| Subject: | Req #79692 [Opn->Csd]: Creating DateTime from unixtime should support fractional seconds | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-241664@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79692&edit=1
ID: 79692
Updated by: derick@php.net
Reported by: eric at ericstern dot com
Summary: Creating DateTime from unixtime should support
fractional seconds
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: Date/time related
Operating System: All
PHP Version: 7.4.7
-Assigned To:
+Assigned To: derick
Block user comment: N
Private report: N
New Comment:
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at
http://www.php.net/downloads.php
Support for this was added in PHP 8.0: https://3v4l.org/96qtZ
Previous Comments:
------------------------------------------------------------------------
[2020-06-12 00:29:17] eric at ericstern dot com
> A unix timestamp by definition does not allow fractions:
> > the number of **seconds** that have elapsed since the Unix epoch
This in no way conflicts with that definition - it states nothing about the number being an integer,
and numerous sources exist showing fractional timestamps (notably including the Wikipedia entry on
unixtime). It does conflict with several touch points in PHP, though, and I'm certainly not
advocating for e.g. modifying the return type of time().
Regardless, I appreciate this being left open and updated to a change request! I missed that option
when opening the issue.
------------------------------------------------------------------------
[2020-06-11 23:47:13] peehaa@php.net
A unix timestamp by definition does not allow fractions:
> the number of **seconds** that have elapsed since the Unix epoch
------------------------------------------------------------------------
[2020-06-11 22:52:33] eric at ericstern dot com
Description:
------------
PHP has supported fractional seconds apparently dating back to 5.2.2 - a bit over 13 years. However
when trying to create a new DateTime or DateTimeImmutable object from a unix timestamp (via
"@1591858149"), non-integer timestamps are not parsed and can throw an exception:
Exception: DateTime::__construct(): Failed to parse time string (@1591858149.085) at
Since
new DateTime() will have a fractional component set based on the system clock,
and other constructor formats support it (example: https://www.php.net/manual/en/class.datetime.php#118608),
this inconsistency seems specific to unix timestamps and should be resolved
Test script:
---------------
https://3v4l.org/54J1b
<?php
$timestamp = 1591858149.085;
$dt = new DateTime("@$timestamp");
$dti = new DateTimeImmutable("@$timestamp");
Expected result:
----------------
No errors; created objects correctly store the microseconds component from the timestamp.
Actual result:
--------------
Exception was thrown.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79692&edit=1