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: 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
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.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Aug 12 21:01:26 2020 UTC