How do I convert datetime to ISO 8601 in PHP?

How do I convert datetime to ISO 8601 in PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

Converting Datetime to ISO 8601 in PHP

When working with timestamps in PHP, you often encounter strings that look like 2010-12-30 23:21:46. While this format is human-readable, it doesn't strictly adhere to the ISO 8601 standard. The critical distinction lies in the timezone information. If your string represents a naive local time or UTC without an explicit offset, most applications and database systems will misinterpret it. To ensure interoperability, you must convert these values to a format that includes the UTC offset, such as 2010-12-30T23:21:46+00:00.

The most robust way to handle this in modern PHP is by leveraging the native DateTime and DateTimeImmutable classes. These objects provide a standardized API for parsing strings, performing arithmetic, and formatting output without relying on deprecated functions like strtotime() or date().

The Modern Approach: DateTime Formatting

To convert a string to ISO 8601, you first need to instantiate a DateTime object. Once the object is created, you can format it using the format() method with the specific pattern required for ISO 8601.

Consider a timestamp that lacks timezone context:

$rawString = '2010-12-30 23:21:46';

// Create a DateTime object
$date = new DateTime($rawString);

// Format to ISO 8601 with offset
$isoString = $date->format('Y-m-d\TH:i:sP');

echo $isoString; 
// Output: 2010-12-30T23:21:46+00:00 (assuming UTC or default timezone)

The format string Y-m-d\TH:i:sP is crucial here. The literal T separates the date and time components, which is a requirement for ISO 8601. The P modifier specifically appends the offset in the +HH:MM or -HH:MM format. If you omit the P, PHP defaults to an uppercase Z only if the timezone is explicitly set to UTC; otherwise, it might output just the time without the offset, which violates strict ISO 8601 rules for non-fixed-timezone contexts.

Handling Timezones Explicitly

A common pitfall occurs when developers assume a string is in UTC but treat it as local time, or vice versa. If your input 2010-12-30 23:21:46 represents a specific moment in New York (EST), converting it to ISO 8601 without adjusting the offset will result in incorrect data for UTC-based systems like AWS S3, Google Cloud Storage, or PostgreSQL.

To handle this correctly, you must set the timezone of your DateTime object before formatting. This ensures the output reflects the actual UTC offset at that moment.

$rawString = '2010-12-30 23:21:46';
$date = new DateTime($rawString, new DateTimeZone('America/New_York'));

// Now format it. The P modifier will show +05:00 if DST is active, or -05:00 if not.
$isoString = $date->format('Y-m-d\TH:i:sP'); 

This approach guarantees that the resulting string accurately represents the point in time relative to UTC, regardless of the source timezone. When you serialize this back out, the receiving application can unambiguously determine the exact moment in history.

Performance Considerations

While DateTime is powerful, it does carry a performance cost compared to older C-based functions. If you are processing millions of records and cannot afford the overhead of object instantiation, you might consider using DateInterval or raw string manipulation. However, these alternatives often sacrifice readability and safety. The DateTime class handles edge cases, such as invalid dates or daylight saving time transitions, automatically.

In high-performance scenarios, a common optimization pattern involves creating a single DateTimeImmutable instance to serve as the formatter if the timezone does not change throughout the request lifecycle. This avoids the overhead of repeated timezone calculations while maintaining immutability, which is safer for concurrent code execution.

Legacy Contexts and Deprecations

If you are constrained by older PHP versions (pre-5.5), you might have relied on date('Y-m-d\TH:i:sP', strtotime($rawString)). While this works in many cases, it is less explicit about timezone handling than the object-oriented approach. As of PHP 8.1, the default timezone behavior has shifted, making explicit timezone management even more critical to avoid silent failures where date_default_timezone_get() returns a value that differs from what the database expects.

For projects using Laravel or similar frameworks, the Eloquent ORM often handles these conversions automatically when interacting with date columns, but understanding the underlying DateTime mechanics is essential when debugging serialization issues or generating API responses directly.

Our network