How to convert a 13 digit Unix Timestamp to Date and time?

How to convert a 13 digit Unix Timestamp to Date and time?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The Millisecond Trap in PHP Timestamps

A standard Unix timestamp represents the number of seconds that have elapsed since the Unix epoch, January 1, 1970, 00:00:00 UTC. When you encounter a string like 1443852054000, it is almost certainly a millisecond timestamp. This value contains three extra digits compared to the standard integer format expected by most PHP date functions. Your previous attempts failed because date() and strtotime() interpret their numeric inputs strictly as seconds. When you pass a 13-digit number directly into these functions, they treat the entire sequence as a time far in the future relative to the current epoch, or they simply ignore the leading digits if the input exceeds the maximum integer size for the system's architecture.

To resolve this, you must normalize the timestamp before processing it. The most reliable method is to perform integer division by 1000. This strips the millisecond precision and leaves you with a valid 10-digit Unix timestamp representing seconds. You can then pass this corrected value directly into your date() function.

$timestamp = 1443852054000; // The 13-digit input
$seconds = (int)($timestamp / 1000); // Convert to seconds
$formattedDate = date('Y-m-d H:i:s', $seconds);
echo $formattedDate;

This approach is robust because it relies on standard arithmetic rather than relying on string manipulation that might fail if the input format varies slightly. Dividing by 1000 effectively truncates the sub-second precision, which is often sufficient for general date and time displays. If you need to preserve milliseconds in your output, you can combine this logic with a specific formatting pattern:

$milliseconds = $timestamp % 1000; // Get the remainder as milliseconds
$seconds = (int)($timestamp / 1000); // Get the seconds part
$formattedDate = date('Y-m-d H:i:s.u', $seconds * 1000 + $milliseconds);

However, a more modern and cleaner approach in PHP 8.1+ is to use DateTimeImmutable or DateTime with the DateTimeZone::UTC context. These classes can handle the normalization internally if you pass the raw string, provided you inform them of the unit used. While strtotime() is convenient, it is notorious for being inconsistent across different PHP versions and environments when dealing with non-standard inputs. Relying on arithmetic division ensures that your code behaves predictably regardless of the underlying OS or PHP version.

If you are working within a Laravel application, you might consider utilizing the helper functions provided by the framework. Laravel's Carbon library is built on top of PHP's native date classes but offers a more intuitive API for handling these conversions. You can pass the raw string to Carbon and explicitly define the unit as milliseconds.

use Illuminate\Support\Carbon;

$carbon = Carbon::createFromTimestampMs(1443852054000);
echo $carbon->format('Y-m-d H:i:s');

Using createFromTimestampMs is significantly safer than trying to parse the string with strtotime(), which might misinterpret the number of digits. This method specifically instructs the library that the input is in milliseconds, eliminating the guesswork. It also handles timezone conversions gracefully if you need to display the time relative to a specific server or user timezone rather than UTC.

Common Pitfalls to Avoid

One frequent mistake developers make when dealing with timestamps from databases like MongoDB or JavaScript environments is assuming the field is always in seconds. Many modern data stores default to storing dates in milliseconds because they align better with 32-bit floating-point precision limits used in JavaScript's Date object. If you blindly apply a 10-second divisor logic without checking the input length, your application will break when it encounters a standard 10-digit timestamp from an older system. Always inspect the magnitude of the incoming data before deciding on your conversion strategy.

Another potential issue arises with integer overflow. On 32-bit systems, a 13-digit number might exceed the maximum value for a signed 32-bit integer. However, PHP handles integers as arbitrary precision (bignums) in recent versions, so simple arithmetic usually works without explicit casting issues unless you are interfacing directly with C-extensions that enforce 32-bit limits. If you are passing this timestamp to an external API or converting it to a JavaScript Date object, remember that the conversion logic must be consistent on both ends; sending milliseconds where seconds were expected will shift your dates back by roughly three hours and fifty minutes every day relative to the intended time.

By normalizing the input first, you ensure that your date formatting logic remains decoupled from the source data's specific unit choice, making your application more resilient to changes in upstream data sources.

Our network