Req #49779 [Opn->Csd]: Add support for microtime to DateTime object construction
| From: | derick@php.net | Date: | Wed, 05 Oct 2016 20:14:39 +0000 |
| Subject: | Req #49779 [Opn->Csd]: Add support for microtime to DateTime object construction | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-204501@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=49779&edit=1
ID: 49779
Updated by: derick@php.net
Reported by: urkle at outoforder dot cc
Summary: Add support for microtime to DateTime object
construction
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: Date/time related
Operating System: CentOS 5
PHP Version: 5.3.0
-Assigned To:
+Assigned To: derick
Block user comment: N
Private report: N
New Comment:
The fix for this bug has been committed.
Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
For Windows:
http://windows.php.net/snapshots/
Thank you for the report, and for helping us make PHP better.
From PHP 7.1.0 RC4, you can do:
new DateTime();
new DateTime::createFromFormat( "U.u", microtime( TRUE ) );
DateTime::setTime() now also accepts an extra 4th argument to set microseconds.
I know it's not *exactly* what you've asked for, but I consider this to be "good
enough" for now.
Previous Comments:
------------------------------------------------------------------------
[2009-10-05 19:03:09] urkle at outoforder dot cc
Description:
------------
Currently the DateTime object construction accepts a strtotime type input only for setting the date
on the object. This means that when trying to get the current time WITH microseconds you have to do
something like this..
<?php
$t = microtime(true);
$micro = sprintf("%06d",($t - floor($t)) * 1000000);
$d = new DateTime( date('Y-m-d H:i:s.'.$micro,$t) );
?>
which is highly inefficient as we are converting a float into a string and then converting it from a
string into the internal format for the DateTime object.
It would be really nice to have a constructor for the DateTime object that would accept a float
argument (defaulting to NOW w/ microseconds)
Possible suggestions would be
factory method
$d = DateTime::TimeWithMicroseconds();
make the constructor accept a float
$d = new DateTime(microtime(true));
modify the setTimestamp method in PHP 5.3 to accept a float.
$d = new DateTime();
$d->setTimestamp(microtime(true));
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=49779&edit=1