Convert hour to PM and AM with Carbon

Convert hour to PM and AM with Carbon

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

Stefan Bogdanescu

Founder & Senior Architect

Converting Hour Format to AM/PM in Carbon

When working with timestamps in PHP, the Carbon extension is often the go-to utility for handling date and time logic. However, switching between 24-hour formats (like 23:00) and 12-hour formats with AM/PM designations isn't immediately obvious via simple arithmetic. If you have a timestamp or just an hour value and need to render it as "11:00 PM" instead of "23:00", Carbon provides specific methods to handle this transformation without manual calculation errors.

The most direct approach is using the format method with the appropriate token. In Carbon, the string format token g:i A (or h:i A) converts a time instance into 12-hour notation followed by the AM or PM suffix. This method expects a Carbon instance as its input. If you are starting with a raw integer representing hours, such as 23, you must first convert that integer into a Carbon instance before applying the formatting string.

use Carbon\Carbon;

// Starting with an hour integer (e.g., 23)
$hour = 23;

// Create a Carbon instance for today at that specific hour
$date = Carbon::today()->setHour($hour)->setMinute(0)->setSecond(0);

// Convert to AM/PM format
$formattedTime = $date->format('g:i A'); 

echo $formattedTime; // Output: 11:00 PM

The logic here relies on Carbon's internal handling of the 24-hour cycle. When you set the hour to 23, Carbon knows this represents one hour before midnight. The format method then performs the modulo arithmetic required to shift from the 24-hour base (0–23) to the 12-hour base (1–12), adding the appropriate suffix based on whether the hour is greater than or equal to 12.

It is important to distinguish between the tokens used for formatting, as mixing them up can lead to unexpected results. The token G outputs hours in 24-hour format without leading zeros (e.g., 23). In contrast, g outputs hours in 12-hour format without leading zeros (e.g., 11). To get a consistent two-digit hour with the AM/PM suffix, you should use h instead of G.

// Using 'G' results in 24-hour output (23:00 PM is technically invalid but might show as 11 PM depending on context)
echo $date->format('G:i A'); // Output: 23:00 PM (Note: G is usually for 24h, so this mixes formats)

// Using 'g' without leading zero
echo $date->format('g:i A'); // Output: 11:00 PM

// Using 'h' with leading zero for consistent display (e.g., 09:00 vs 9:00)
echo $date->format('h:i A'); // Output: 11:00 PM

A common pitfall is attempting to manipulate the hour string directly using PHP's explode and strtotime functions after converting to AM/PM. This introduces unnecessary complexity and potential timezone mismatches. Carbon abstracts away the timezone offset calculations, ensuring that whether your server is in UTC or EST, the local representation remains accurate once the instance is created with the correct context.

If you are building a Laravel application, these patterns integrate seamlessly into your blade templates or controller logic. You can pass the formatted string directly to view components without needing to preprocess every timestamp manually. While external resources like https://laravelcompany.com often discuss broader application architectures, specific utility functions like Carbon's formatting remain consistent across versions and do not require additional configuration unless you are handling custom locales that alter how "AM" or "PM" is spelled in your target language.

Our network