1

How can I use the following 16-digit timestamp (from an XML file) with PHP's date() function?

1295076698126000  // 15-01-2011 08:31:38.126
1286697695521000  // 10-10-2010 10:01:35.521
Kristoffer Bohmann
  • 3,986
  • 3
  • 28
  • 35

1 Answers1

5

Those timestamps are in microseconds. However, since PHP uses integers for timestamps in seconds with date(), you won't be able to obtain the microsecond value. You're still able to print the rest of the date by dividing the timestamp by a million (1 million microseconds = 1 second), and passing the quotient to date():

// "u" will always be printed as 000000 regardless of actual microseconds
echo date('d-m-Y H:i:s.u', 1295076698126000 / 1000000);

EDIT: Hacky, but you can perform manual arithmetic to get the milliseconds and output it separately as a workaround, like this:

$xml_timestamp = 1295076698126000;
$seconds = $xml_timestamp / 1000000;
$microseconds = $seconds - floor($seconds);
$seconds = floor($seconds);

// 1 millisecond = 1000 microseconds
// Milliseconds, because your desired output is 3 decimal places long, not 6
$milliseconds = round($microseconds * 1000);

$format = 'd-m-Y H:i:s.' . sprintf('%03d', $milliseconds);
echo date($format, $seconds);

For reusability the DateTime class is a good option. Or, a custom function:

function date_milliseconds($format, $timestamp = NULL) {
    $seconds = ($timestamp === NULL) ? microtime(true) : $timestamp / 1000000;
    $microseconds = $seconds - floor($seconds);
    $seconds = floor($seconds);
    $milliseconds = round($microseconds * 1000);

    $format = preg_replace('/(?<!\\\\)u/', sprintf('%03d', $milliseconds), $format);
    return date($format, $seconds);
}

echo date_milliseconds('d-m-T H:i:s.u', floatval($xml_timestamp));
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Note that you *can* use "u" with the `DateTime` object, as per Edward Rudd's comment on the `date()` manual page: http://uk3.php.net/manual/en/function.date.php#93891 But this isn't actually very useful, since I can't see a simple way to *set* a microsecond timestamp (`setTimestamp()` seems to cast the input to int). – John Carter Jan 15 '11 at 10:45
  • Can you make the code dynamic? `echo $timestamp*1` always gives the same number (2147483647), see: http://stackoverflow.com/questions/573692/is-the-size-of-an-array-constrained-by-the-upper-limit-of-int-2147483647 – Kristoffer Bohmann Jan 17 '11 at 06:59
  • @Kristoffer: What version of PHP are you on? PHP turns `$timestamp` into a float for me when it exceeds the 32-bit signed int limit. – BoltClock Jan 17 '11 at 07:15
  • Current version v5.3.5 - with SimpleXML to parse the XML. – Kristoffer Bohmann Jan 17 '11 at 19:03
  • If you `var_dump()` the timestamps immediately after obtaining them with SimpleXML what do you get? – BoltClock Jan 17 '11 at 19:08
  • And: `echo var_dump($timestamp*1);` outputs an integer (example: $timestamp = 1286911435434000) - please notice this may be caused by SimpleXML. – Kristoffer Bohmann Jan 17 '11 at 19:09
  • Well, whether it's `int(1286911435434000)` or `float(1286911435434000)`, it looks like it's not overflowing. If you can dump it from SimpleXML without affecting its numeric value you should be able to assign it to a new variable, then pass it to `date_milliseconds()` (unfortunately I can't test the SimpleXML part right now). – BoltClock Jan 17 '11 at 19:13