Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php: convert milliseconds to date

Tags:

php

datetime

time

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?

like image 267
Jeff Winkworth Avatar asked Feb 17 '09 17:02

Jeff Winkworth


People also ask

How to convert milliseconds to date time in php?

For the conversion itself, I use this line: $date = date('d-m-Y H:i:s', $millis / 1000);

How do you convert milliseconds to date?

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.

What is Strtotime PHP?

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.


2 Answers

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

like image 134
Patrick Glandien Avatar answered Sep 25 '22 13:09

Patrick Glandien


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); 
like image 24
DanielOpaluwa Avatar answered Sep 23 '22 13:09

DanielOpaluwa