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?
For the conversion itself, I use this line: $date = date('d-m-Y H:i:s', $millis / 1000);
This is the number of seconds since the 1970 epoch. To convert seconds to milliseconds, you need to multiply the number of seconds by 1000. To convert a Date to milliseconds, you could just call timeIntervalSince1970 and multiply it by 1000 every time.
The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.
You are already doing it right, 1227643821 is simply not 02-12-2008, it is indeed 25-11-2008.
I just added H:i:s like in the below example:
$mil = 1227643821310; $seconds = $mil / 1000; echo date("d/m/Y H:i:s", $seconds);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With