php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79692 Creating DateTime from unixtime should support fractional seconds
Submitted: 2020-06-11 22:52 UTC Modified: 2022-06-04 17:22 UTC
From: eric at ericstern dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 7.4.7 OS: All
Private report: No CVE-ID: None
 [2020-06-11 22:52 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-06-11 23:47 UTC] peehaa@php.net
-Status: Open +Status: Not a bug
 [2020-06-11 23:47 UTC] 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 23:48 UTC] derick@php.net
-Status: Not a bug +Status: Open -Type: Bug +Type: Feature/Change Request
 [2020-06-12 00:29 UTC] 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.
 [2021-11-11 11:13 UTC] nikic@php.net
-Package: date_time +Package: Date/time related
 [2022-06-04 17:22 UTC] derick@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: derick
 [2022-06-04 17:22 UTC] derick@php.net
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
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sat Sep 24 19:05:54 2022 UTC