PHP: Convert timestamp in milliseconds to seconds.

This is a short guide on how to convert a timestamp in milliseconds into a timestamp in seconds using PHP. As you probably already know, certain programming languages and services will output their timestamps in milliseconds. i.e. The number of milliseconds that have passed since January 1, 1970 .

As a result, this can be a small bit of a problem for us PHP developers, as we primarily use seconds.

The other day, I was using PHP to extract location history from Google’s location history file when I came across a field called timestampMs. This field contained a timestamp in milliseconds.

One of the timestamps in my file was: 1448204926730

To convert this into seconds, I simply divided the milliseconds by 1,000:

//Convert milliseconds into seconds by
//dividing the milliseconds by 1000.
$location['timestampMs'] / 1000;

This works because there are 1,000 milliseconds in a seconds.

After converting the timestamp in question, you can simply format it using PHP’s date function:

//Divide by 1,0000
$timestampSeconds = $location['timestampMs'] / 1000;

//Format it into a human-readable datetime.
$formatted = date("D d/m/Y H:i:s", $timestampSeconds);

//Print it out
echo $formatted;

In the code snippet above, I passed the new converted timestamp in as the second parameter to date. The result in my case led to:

Sun 22/11/2015 15:08:46


Hopefully, you found this short guide useful.