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");
Advertisement
Answer
/! EDIT /!
I now use https://github.com/briannesbitt/Carbon, the rest of this answer is just here for historical reasons.
END EDIT
I decided to extend the class DateTime
using the tips you all gave me.
The constructor takes a float (from microtime
) or nothing (in this case it will be initialized with the current “micro-timestamp”).
I also overrided 2 functions that were important : setTimestamp
and getTimestamp
.
Unfortunately, I couldn’t solve the performances issue, although it’s not as slow as I thought.
Here’s the whole class :
<?php
class MicroDateTime extends DateTime
{
public $microseconds = 0;
public function __construct($time = 'now')
{
if ($time == 'now')
$time = microtime(true);
if (is_float($time + 0)) // "+ 0" implicitly converts $time to a numeric value
{
list($ts, $ms) = explode('.', $time);
parent::__construct(date('Y-m-d H:i:s.', $ts).$ms);
$this->microseconds = $time - (int)$time;
}
else
throw new Exception('Incorrect value for time "'.print_r($time, true).'"');
}
public function setTimestamp($timestamp)
{
parent::setTimestamp($timestamp);
$this->microseconds = $timestamp - (int)$timestamp;
}
public function getTimestamp()
{
return parent::getTimestamp() + $this->microseconds;
}
}