views:

3527

answers:

3

I Have a string that is equal to a date, represented as the number of milliseconds since the Unix epoch.

I am trying to output it into d-m-Y.

The string I was given was "1227643821310", and I am told that the result should be equal to 2-12-2008, but I keep getting a result of 25-11-2008

My code is as follows:

$mil = 1227643821310;
$seconds = $mil / 1000;
echo date("d-m-Y", $seconds);

Any ideas as to why this might be?

+6  A: 

You are already doing it right, 1227643821 is simply not 02-12-2008, it is indeed 25-11-2008.

Patrick Daryll Glandien
Agreed. See here for assurance: http://www.onlineconversion.com/unix_time.htm
ryeguy
You are indeed correct - it turns out the source I was given was wrong.
Jeff Winkworth
A: 

The only thing I can think of is try rounding off the decimal portion before converting it to a date. If that doesn't change the result, then the result is correct.

Scott
Already tried, it results in the same.
Patrick Daryll Glandien
A: 

Jeff, the important thing to understand when dealing with timestamps: they represent time which have passed from 0:00:00 01.01.1970 in GMT, not in your timezone (unless you are youself in GMT, of course).

1227643821 indeed represents the GMT time of 20:10:21 25.11.2008.

This is November 25th, 2008 in most of the world, however in timezones to the east of Moscow (and in the Moscow timezone itself in summer because of daylight savings time) it’s already November 26th. Since the most “extreme” east time zone is GMT+14, there’s no place in the world where the timestamp of 1227643821 can represent a date later then the 26th.

Author of the original value may have somehow mistaken when dealing with timezones. Or just plain mistaken. For example, when calculating the value, added seconds instead of milliseconds at some step.

Ilya Birman