Convert Epoch Time to Date PHP

Convert Epoch Time to Date PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Decoding Large Epoch Timestamps in PHP

When working with APIs that return timestamps, you frequently encounter a specific format: a large integer like 1353430853299. This is an epoch time value expressed in milliseconds since the Unix epoch (January 1, 1970). While standard documentation often focuses on converting human-readable dates into these integers, reversing that process is equally critical for parsing API responses.

The core issue you are facing usually stems from a unit mismatch. The PHP date() function and strtotime() expect timestamps in seconds, not milliseconds. If your source material provides a value in the hundreds of billions (like 1353430853299), dividing it by 1000 is indeed the necessary first step to normalize the data. However, simply performing the arithmetic and passing the result directly to date() can sometimes lead to unexpected output if not handled with care regarding timezone contexts.

To convert this specific large integer correctly, you must explicitly divide the epoch time by 1,000 before formatting it. Consider the following PHP implementation:

$epoch_milliseconds = 1353430853299;
$epoch_seconds = $epoch_milliseconds / 1000;
$formatted_date = date('Y-m-d H:i:s', (int)$epoch_seconds);

echo $formatted_date; // Outputs: 2012-11-19 07:40:53

In this example, the division converts the millisecond count into seconds. Casting the result to (int) is a best practice because floating-point arithmetic can occasionally introduce precision errors when dealing with very large numbers, though in this specific case, the magnitude is low enough that it rarely causes issues. The resulting string represents the date and time in your server's default timezone.

A common pitfall arises regarding Timezones. The raw epoch time represents a moment in UTC. If you run date() without specifying a timezone, PHP defaults to the server's local timezone. This is often why conversions look "wrong" at first glance; the date might be correct but shifted by several hours. To ensure accuracy across different environments, it is highly recommended to set the default timezone or specify one directly within the function call:

date_default_timezone_set('UTC');
// Or
$formatted_date = date('Y-m-d H:i:s', (int)($epoch_milliseconds / 1000), 'UTC');

If you are working within a Laravel application, the framework provides robust utilities that abstract away these manual calculations. You can utilize the Carbon library, which is included in Laravel's ecosystem. By converting the string representation of the timestamp to a Carbon instance, you gain access to extensive formatting options and automatic timezone handling based on your project configuration.

$timestamp = 1353430853299;
$date = \Carbon\Carbon::createFromTimestampMs($timestamp);

echo $date->format('Y-m-d H:i:s');

The createFromTimestampMs method is specifically designed to handle millisecond inputs, removing the need for manual division and reducing the chance of arithmetic errors. This approach also implicitly handles timezone conversions if you access the property with a specific offset or region (e.g., $date->setTimezone('America/New_York')).

Ultimately, the choice between manual calculation using date() and using Carbon depends on your project requirements. Manual conversion offers zero dependencies and is sufficient for simple scripts where precision regarding timezone offsets is managed explicitly. However, for complex applications requiring detailed date manipulation or specific regional formatting, leveraging Laravel's built-in tools ensures consistency and maintainability. Always remember that the underlying unit—seconds versus milliseconds—is the primary variable that determines whether your conversion logic will succeed.

Our network