|  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: 2020-06-12 00:29 UTC
From: eric at ericstern dot com Assigned:
Status: Open Package: date_time (PECL)
PHP Version: 7.4.7 OS: All
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2020-06-11 22:52 UTC] eric at ericstern dot com
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:, this inconsistency seems specific to unix timestamps and should be resolved

Test script:


$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.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-06-11 23:47 UTC]
-Status: Open +Status: Not a bug
 [2020-06-11 23:47 UTC]
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]
-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.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Jun 25 03:01:23 2021 UTC