views:

130

answers:

2

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?

+2  A: 

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); 
Pekka
Whats the best way to script it?
Craig Ward
Umm, how is removing the last three digits different from dividing by 1000?
Gordon
@Gordon Denkfehler :) corrected.
Pekka
@Craig I updated my answer.
Pekka
I have tried using $date = date('Y-m-d H:i:s', $seconds); but it still comes out as 1970-01-25 20:31:23, in fact the feed contains multiple items which each have a time stamp, even though the timestamp is different for each one the date on each is 1970-01-25 20:31:23.
Craig Ward
@Craig that's hard to believe. I am suspecting the incoming timestamps have a non-numeric character in them. Are you 100% sure they look like you posted them? WHat happens if you do a `$mil = (int)$timestamp;`?
Pekka
+2  A: 
$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)

VolkerK
Talking about SimpleXml quirks again ;)
Gordon
Hi Guys, Managed to get it working using`$timestamp = $xml->entry[$i]->attributes('http://www.google.com/schemas/reader/atom/')->{'crawl-timestamp-msec'}; $tsmod = substr($timestamp, 0, -3); $date = date('Y-m-d H:i:s', $tsmod);`Cheers for all the help guys
Craig Ward