Format time in PHP using 12-hour, 24-hour and locale conventions

How PHP formats times in 12-hour and 24-hour notation, how to add AM/PM, when to use IntlDateFormatter for locale conventions, and what breaks when output is copied.

PHP gives you two separate ways to turn a time into text. The first is the fixed-format route, through date() and DateTimeInterface::format(), where you spell out every piece yourself. The second is the locale-aware route, through IntlDateFormatter from the intl extension, where the locale decides the clock convention. This review covers what each route actually changes, which one suits which job, and where the formatted string stops being reliable once it leaves your code.

Short verdict: which approach fits your use

Use format() with an explicit pattern such as 'H:i' or 'g:i A' when one fixed convention is required. Typical cases are logs, CSV exports, internal tools, and interfaces aimed at a single audience. You get predictable, ASCII-only output that you can parse back with the same pattern. The cost is that the output is always English and always in the convention you hard-coded, whatever the reader's locale.

Use IntlDateFormatter when people in different regions will read the time. Its benefit is that the locale picks 12-hour or 24-hour notation, the day-period wording, and the separators. The tradeoff that usually decides the choice is that the exact output depends on the ICU and CLDR data bundled with your PHP build. That output can include non-ASCII characters and can change between versions, so it is a poor choice for anything a program must parse.

A plain, fixed format is the better choice whenever the string will be stored, compared, sorted, parsed, or exchanged between systems. In those cases, keep the time as a DateTimeImmutable or an ISO 8601 string and format it for people only at display time.

What formatting actually changes

Formatting does not alter the stored time. A DateTimeImmutable holds a point in time plus a time zone, and format() produces a new string from it. Choosing 12-hour instead of 24-hour notation only changes how that string looks; the underlying value stays the same. Problems start when the display string is saved and later treated as data. At that point, information can be lost: the date, the time zone, seconds trimmed by the pattern, or the AM/PM marker if it was dropped.

"Format" means two different pattern languages in PHP, and they are easy to mix up. date() and DateTimeInterface::format() use PHP's own single-letter codes, where 'i' means minutes and 'A' means AM/PM. IntlDateFormatter uses ICU pattern syntax, where 'mm' means minutes and 'a' means the day period. A pattern written for one will produce wrong output in the other.

PHP format character | Meaning | Example for 9:05 in the morning
H | 24-hour, leading zero (00–23) | 09
G | 24-hour, no leading zero (0–23) | 9
h | 12-hour, leading zero (01–12) | 09
g | 12-hour, no leading zero (1–12) | 9
i | Minutes, leading zero | 05
s | Seconds, leading zero | 09
A | Uppercase AM or PM | AM
a | Lowercase am or pm | am
T | Time zone abbreviation | depends on zone and date
P | UTC offset with colon | depends on zone and date

Fixed 24-hour and 12-hour output with AM/PM

<?php
$tz = new DateTimeZone('America/New_York');
$t  = new DateTimeImmutable('2026-03-14 09:05:09', $tz);

echo $t->format('H:i'), PHP_EOL;      // 09:05
echo $t->format('G:i'), PHP_EOL;      // 9:05
echo $t->format('h:i A'), PHP_EOL;    // 09:05 AM
echo $t->format('g:i a'), PHP_EOL;    // 9:05 am
echo $t->format('g:i:s A T'), PHP_EOL; // 9:05:09 AM EDT

// Escape letters you want printed literally
echo $t->format('H\hi'), PHP_EOL;     // 09h05

The outputs in the comments follow directly from the documented meaning of each format character. The EDT abbreviation applies because daylight saving time is in effect in New York on that date. Write patterns in single quotes so that backslash escapes reach format() unchanged. format() always produces English AM/PM markers and ignores setlocale(). Styles such as "a.m." or "p.m." with periods are not built in. You would have to construct them yourself, for example by mapping the value of 'a'.

Locale conventions with IntlDateFormatter

For a locale-appropriate time, pass a locale and a time style to IntlDateFormatter, with the date style set to NONE. Under CLDR conventions, en_US typically yields a 12-hour time with a day-period marker, while locales such as de_DE or ja_JP typically yield 24-hour notation. The exact characters come from the ICU data in your build. To see precisely what your server produces, run the code there.

<?php
$t = new DateTimeImmutable('2026-03-14 14:05', new DateTimeZone('UTC'));

foreach (['en_US', 'de_DE', 'ja_JP'] as $locale) {
    $fmt = new IntlDateFormatter(
        $locale,
        IntlDateFormatter::NONE,   // no date part
        IntlDateFormatter::SHORT,  // short time
        'UTC'
    );
    echo $locale, ': ', $fmt->format($t), PHP_EOL;
}

Since PHP 8.1, IntlDatePatternGenerator can build a locale-correct pattern from a skeleton. In a skeleton, 'j' means "the hour format this locale prefers", 'H' forces 24-hour notation, and 'h' forces 12-hour notation. The generator then arranges separators and the day period to suit the locale. This is the cleaner way to honor a user's 24-hour preference without abandoning their locale's other conventions.

<?php
$locale  = 'en_US';
$gen     = new IntlDatePatternGenerator($locale);

$preferred = $gen->getBestPattern('jmm'); // locale's usual hour cycle
$forced24  = $gen->getBestPattern('Hmm'); // 24-hour, locale-arranged

$fmt = new IntlDateFormatter(
    $locale,
    IntlDateFormatter::NONE,
    IntlDateFormatter::NONE,
    'America/Chicago',
    null,
    $forced24
);
echo $fmt->format(new DateTimeImmutable('now'));

Avoid strftime() for new code. It was deprecated in PHP 8.1. It also depends on setlocale(), which applies process-wide, and on the locales installed on the operating system, so the same code can print differently on different servers.

Time zone is part of the conversion

date() and new DateTime() without an explicit zone use the default time zone, which comes from the date.timezone setting or date_default_timezone_set(). IntlDateFormatter accepts its own time zone argument and converts to that zone before formatting. A common working pattern has three steps: store times in UTC, convert to the reader's zone with setTimezone() or the formatter's zone argument, and only then format. If the zone matters to the reader, include it in the output with T or P, or with the ICU 'z' or 'zzzz' letters.

Will the result stay readable after copying or sharing?

Output from format() is plain ASCII built from the characters in your pattern, so it copies and pastes cleanly. The usual risk with it is ambiguity rather than corruption. The string "9:05" without a marker could mean morning or evening. "12:00 AM" for midnight and "12:00 PM" for noon are routinely misread. In 24-hour notation, the H code writes midnight as "00:00" and never produces "24:00".

Locale-aware output can contain characters that look like ordinary spaces but are not. Recent CLDR data uses a narrow no-break space (U+202F) between the time and the AM/PM marker in English formats. Other locales may use other special spaces or non-Latin day-period words. Strings containing these characters can fail exact string comparisons, break naive parsers, and fail input validation that expects a regular space. If a strictly ASCII string is required, either replace the character, or use a fixed format() pattern instead.

<?php
// Normalize narrow and regular no-break spaces to a plain space
$plain = str_replace(["\u{202F}", "\u{00A0}"], ' ', $formatted);

Parsing a formatted time back is reliable only with the exact pattern that produced it. For example, DateTimeImmutable::createFromFormat('g:i A', $s) reads a 'g:i A' string, and you must supply the zone yourself. IntlDateFormatter::parse() exists, but because its expected input depends on the locale and data version, treat locale-formatted strings as display-only. For anything that travels between systems, use ISO 8601, for example format(DATE_ATOM) or format('c'), which includes the date and the UTC offset.

On web pages, a common way to serve both humans and machines is to wrap the localized text in an HTML time element whose datetime attribute holds the ISO 8601 value. The visible text can follow the reader's locale while scripts, search tools, and other software read the unambiguous value. Search and assistive technology support for this element varies by product. The supplied material does not include tests of specific screen readers or search engines, so check the tools your audience actually uses rather than assuming support.

Pronunciation also deserves a check. A time with its day-period marker, or a full 24-hour time, gives a screen reader or a translator enough to work with. A bare 12-hour time does not. How a given screen reader reads "14:05" or "2:05 PM" was not tested here, so verify it with the assistive tools your audience uses.

When to prefer a plain fixed format

  • Logs, audit trails, and data files: use 'Y-m-d H:i:s' or ISO 8601, ideally in UTC.
  • Values that will be sorted or compared as strings: use zero-padded 24-hour notation (H rather than G) so the text order matches the time order.
  • APIs and messages between systems: use ISO 8601 with an offset, never a localized string.
  • Interfaces for a single known audience: an explicit pattern such as 'g:i A' for US readers is simpler and more predictable than intl.
  • Environments without the intl extension: format() is part of PHP core, while IntlDateFormatter requires the intl extension to be installed and enabled.

Limits of this review

The fixed-format examples follow from PHP's documented format characters. The locale outputs were not reproduced here, because they depend on the ICU and CLDR version linked into each PHP build. No measurements of performance, screen reader pronunciation, or behavior in specific chat, email, or office applications were available, so the copy-and-paste guidance above describes documented character behavior rather than tested results in those destinations.