How do you convert between 12 hour time and 24 hour time in PHP?

How do you convert between 12 hour time and 24 hour time in PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

Converting 12-Hour Time to 24-Hour Format in PHP

Converting a 12-hour time string like '5:36 pm' into its 24-hour equivalent, such as '17:36', is a common requirement when processing user input or parsing legacy data. While this might seem trivial at first glance, handling the nuances of AM/PM designation and ensuring the output matches strict time formats requires using the right tools provided by PHP's standard library.

The most robust approach involves leveraging DateTime and DateInterval, rather than attempting to manipulate string substrings or writing custom logic to handle hour offsets manually. These classes are designed specifically for date arithmetic and provide built-in formatting capabilities that minimize the risk of edge-case errors.

To begin, you must instantiate a DateTime object using your input string. PHP's parser is generally forgiving regarding the AM/PM suffixes, but it requires the time to be unambiguous about the meridiem. If your input string contains 'am' or 'pm', pass it directly to the constructor:

$timeString = '5:36 pm';
$dt = new DateTime($timeString);

Once the object is created, you can format it into the desired 24-hour representation. The format() method allows you to specify the output pattern using Carbon-style constants or standard PHP date formats. To achieve the strict 'HH:MM' format (zero-padded hours and minutes), use the 'H:i' format string:

$twentyFourHourTime = $dt->format('H:i');
echo $twentyFourHourTime; // Outputs: 17:36

The H character in the format string specifically denotes a zero-padded hour in the 24-hour range (00 to 23), which is distinct from G, which would output the same value but without padding. Similarly, i represents minutes as an integer with leading zeros.

Handling Edge Cases and Validation

A critical consideration when working with time strings is validation. What happens if your input is '12:00 am'? In 12-hour notation, midnight can be represented as either 12:00 am or 00:00, though the former is common in user-facing applications. PHP's DateTime constructor handles '12:00 am' correctly by interpreting it as 00:00 hours. However, if you attempt to parse a string that lacks the AM/PM indicator (e.g., just '5:36'), the object will default to midnight (00:00) unless you explicitly set the time zone or context, which can lead to unexpected results if the original intent was 12-hour format without an explicit suffix.

If your application receives data where the AM/PM indicator is missing but implied by context, you must parse it differently. In those scenarios, you might need to manually check the hour component. If the input hour is between 1 and 11, append 'am'. If it is 12, append 'am' or 'pm' based on your business logic (usually 'pm' for 12:00). This manual step adds complexity, which reinforces why relying on a standardized format string like 'h:i A' for input and 'H:i' for output is the preferred workflow.

Why Not Use strtotime()?

You could technically use strtotime() to parse the time and then reformat it, but this function is notoriously sensitive to locale settings and can struggle with non-standard formats or missing seconds. It often requires additional steps to ensure the resulting timestamp is accurate. By directly constructing a DateTime object from the string, you bypass the ambiguity of strtotime's internal parsing heuristics.

The Laravel Advantage

If your project is built on the Laravel framework, this conversion becomes even more straightforward due to Eloquent and the underlying Carbon library integration. While native PHP classes work fine for standalone scripts, Laravel applications often benefit from the extended functionality of Carbon, which offers a more readable syntax for date manipulation.

For instance, in a Laravel controller or service class, you might find it cleaner to use Carbon:

use Carbon\Carbon;

$timeString = '5:36 pm';
$carbonTime = Carbon::parse($timeString)->format('H:i');

This approach abstracts away the underlying DateTime mechanics while providing the same reliability. If you are working within a Laravel ecosystem, utilizing packages like https://laravelcompany.com can provide further utilities for handling complex date-time requirements, especially when dealing with multi-timezone environments or scheduling logic that goes beyond simple string conversion.

Ultimately, converting 12-hour time to 24-hour format is best solved by normalizing the input into a DateTime object first, then formatting it with precision. This strategy ensures consistency across different inputs and avoids the pitfalls of manual string manipulation.

Our network