Pulling a timestamp from an XML feed with PHP but seem to be to many digits
-
21-09-2019 - |
Question
I am pulling a timestamp from a feed and it gives 12 digits (1269088723811). When I convert it, it comes out as
1901-12-13 20:45:52,
but if I put the timestamp into http://www.epochconverter.com/ it comes out as
Sat, 20 Mar 2010 12:38:43 GMT, which is the correct time.
epochconverter.com mentions that it maybe in milliseconds so I have amended the script to take care of it using
$mil = $timestamp;
$seconds = $mil / 1000;
$date = date('Y-m-d H:i:s', date($seconds));
but it still converts the date wrong, 1970-01-25 20:31:23.
What am I doing wrong?
Solution
This seems to be a trimmed microtime() output. The only thing you seem to be doing wrong is using date($seconds)
instead of the raw $seconds
. Try
$date = date('Y-m-d H:i:s', $seconds);
OTHER TIPS
$xml = new SimpleXMLElement('<foo><a>1269088723811</a></foo>');
echo date('Y-m-d H:i:s', (float)$xml->a / 1000);
and
$xml = new SimpleXMLElement('<foo><a>1269088723811</a></foo>');
$ts = substr($xml->a, 0, -3);
echo date('Y-m-d H:i:s', $ts);
both print 2010-03-20 07:38:43
(on my Europe/Berlin machine)