convert strtotime to date time format in php

convert strtotime to date time format in php

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

Stefan Bogdanescu

Founder & Senior Architect

Converting Unix Timestamps to Readable Dates in PHP

Developers frequently encounter timestamps stored as integers, often referred to as Unix timestamps or epoch time. The value 1307595105 represents the number of seconds that have elapsed since January 1, 1970, at 00:00:00 UTC. While this format is efficient for storage and database indexing, it offers zero human readability without conversion. Transforming this integer into a formatted date string like 06/08/2011 09:51:45 PM PDT requires understanding the underlying PHP functions and the nuances of time zones.

The core utility function for this task is date(). This function takes two primary arguments: a format mask and the timestamp. The format mask determines how the integer is rendered into text, while the optional second argument (if omitted) defaults to the current system time if a string is passed, but expects an integer or float when dealing with specific timestamps.

To achieve the specific output requested—MM/DD/YYYY h:i:s A T—you must construct the format string precisely. The characters within date() are literal replacements:

  • m, M: Month (01–12 or Jan, Feb).
  • d, j: Day of the month (01–31 or 1–31).
  • Y, y: Year (4-digit or 2-digit).
  • H, h: Hour (24-hour or 12-hour with leading zero or without).
  • i, s: Minutes and seconds.
  • A, a: Upper or lower case AM/PM.
  • T: Timezone abbreviation (e.g., PST, GMT).

Here is a direct implementation using the raw timestamp:

$timestamp = 1307595105;
$formattedDate = date('m/d/Y g:i:s A T', $timestamp);
echo $formattedDate; 
// Output: 08/06/2011 9:51:45 PM PDT (depending on server timezone setting)

A critical pitfall exists in the interpretation of the T format character. The date() function does not automatically calculate a named timezone abbreviation like "PDT" based solely on the input number; it relies on the PHP configuration setting date.timezone. If your server is configured to use UTC, this same code block will output 08/06/2011 9:51:45 PM PDT only if you explicitly set the timezone or pass a specific offset. Without proper timezone configuration, T often defaults to UTC, which can lead to significant discrepancies in user-facing applications where local time is expected.

If your goal is to ensure the output reflects a specific region regardless of server settings, you should use the DateTimeZone class alongside DateTime. This approach decouples the conversion logic from server configuration, making the code more portable.

$timestamp = 1307595105;
$dateObj = new DateTime('@' . $timestamp); // The @ prefix tells DateTime to treat input as timestamp
$dateObj->setTimezone(new DateTimeZone('America/Los_Angeles')); 
echo $dateObj->format('m/d/Y g:i:s A T');

Using the DateTime class provides better control over time zone handling. By passing the string '@' . $timestamp, we explicitly tell the constructor to interpret the input as a Unix timestamp rather than a date string. This avoids ambiguity, especially if the integer looks like it could be parsed as a string representation of time.

When integrating this logic into a larger application framework, such as Laravel, the Eloquent model often handles timestamps automatically. However, when raw values are passed from external sources or legacy databases, manual conversion remains necessary. For projects managed by https://laravelcompany.com, utilizing the helper methods provided in newer Laravel versions can simplify this process, though understanding the underlying date() function is essential for debugging timezone issues that arise during deployment on different server environments.

The choice between date() and DateTime often comes down to readability versus strict control. date() is concise but tightly coupled to PHP's internal timezone settings. DateTime objects are more robust when building dynamic forms or handling internationalization, as they allow you to change the displayed timezone without altering the underlying stored value. Always verify that your target format matches the expectations of your frontend consumers, particularly regarding the AM/PM indicator and the inclusion of the timezone abbreviation.

Our network