In my code, I'm using DateTime
objects to manipulate dates, then convert them to timestamp in order to save them in some JSON files.
For some reasons, I want to have the same thing as DateTime (or something close), but with microseconds precision (that I would convert to float when inserting inside the JSON files).
My question is : is there a PHP object that is like DateTime
, but can handle microseconds too ?
The goal is to be able to manipulate microtimes with objects.
In the date() documentation, there is something that indicates that DateTime can be created with microseconds, but I wasn't able to find how.
u Microseconds (added in PHP 5.2.2). Note that date() will always generate 000000 since it takes an integer parameter, whereas DateTime::format() does support microseconds if DateTime was created with microseconds.
I have tried to set the timestamp of a DateTime object with a floating value (microtime(true)
), but it doesn't work (I think it converts the timestamp to an int, causing the loss of the microseconds).
Here is how i tried
$dt = new DateTime(); $dt->setTimestamp(3.4); // I replaced 3.4 by microtime(true), this is just to give an example var_dump($dt); var_dump($dt->format('u'));
The .4
is not taken into account as you can see here (even though we can use the u
format, which corresponds to the microseconds).
object(DateTime)[1] public 'date' => string '1970-01-01 01:00:03' (length=19) public 'timezone_type' => int 3 public 'timezone' => string 'Europe/Berlin' (length=13) string '000000' (length=6)
EDIT : I saw this code, which allows to add microseconds to a DateTime, but I would need to apply a lot of modifications to the microtime before creating the DateTime. Since I will use this a lot, I want to do as little modifications to the microtime as possible before getting the "microtime object".
$d = new DateTime("15-07-2014 18:30:00.111111");
Here's a very simple method of creating a DateTime object that includes microtime.
I didn't delve into this question too deeply so if I missed something I apologize but hope you find this helpful.
$date = DateTime::createFromFormat('U.u', microtime(TRUE)); var_dump($date->format('Y-m-d H:i:s.u'));
I tested it out and tried various other ways to make this work that seemed logical but this was the sole method that worked for PHP versions prior to 7.1.
However there was a problem, it was returning the correct time portion but not the correct day portion (because of UTC time most likely) Here's what I did (still seems simpler IMHO):
$dateObj = DateTime::createFromFormat('U.u', microtime(TRUE)); $dateObj->setTimeZone(new DateTimeZone('America/Denver')); var_dump($dateObj->format('Y-m-d H:i:s:u'));
Here's a working example: http://sandbox.onlinephpfunctions.com/code/66f20107d4adf87c90b5c8c914393d4edef180a2
UPDATE
As pointed out in comments, as of PHP 7.1, the method recommended by Planplan appears to be superior to the one shown above.
So, again for PHP 7.1 and later it may be better to use the below code instead of the above:
$dateObj = DateTime::createFromFormat('0.u00 U', microtime()); $dateObj->setTimeZone(new DateTimeZone('America/Denver')); var_dump($dateObj->format('Y-m-d H:i:s:u'));
Please be aware that the above works only for PHP versions 7.1 and above. Previous versions of PHP will return 0s in place of the microtime, therefore losing all microtime data.
Here's an updated sandbox showing both: http://sandbox.onlinephpfunctions.com/code/a88522835fdad4ae928d023a44b721e392a3295e
NOTE: in testing the above sandbox I did not ever see the microtime(TRUE) failure which Planplan mentioned that he experienced. The updated method does, however, appear to record a higher level of precision as suggested by KristopherWindsor.
NOTE2: Please be aware that there may be rare cases where either approach will fail because of an underlying decision made regarding the handling of microseconds in PHP DateTime code. Either:
Thanks for the headsup Sz. (see comments).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With