How to convert the time from AM/PM to 24 hour format in PHP?

How to convert the time from AM/PM to 24 hour format in PHP?

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

Stefan Bogdanescu

Founder & Senior Architect

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

Handling time formats is a common requirement when processing user input or integrating with external APIs. You might receive timestamps like 09:15 AM or 04:25 PM, but your database schema or downstream application expects the standard 24-hour format (09:15, 16:25). While manual string manipulation works, it quickly becomes brittle and hard to maintain.

Your current approach uses str_replace to strip the " AM" suffix and conditional logic to handle " PM". This pattern is prone to edge cases. For instance, what happens if a user inputs 12:00 PM? Should that be 12:00 or 12:00? Does your logic account for midnight (12 AM) versus noon (12 PM)? Hard-coding string replacements often fails because it doesn't understand the semantics of the time, only the characters present in the string.

PHP provides built-in functions that treat these strings as valid date/time objects, allowing you to rely on the language engine to handle the conversion logic rather than writing your own parser. The most straightforward way to convert a 12-hour string to a 24-hour timestamp is using strtotime() combined with date().

You can parse an AM/PM string directly:

$time = '04:25 PM';
$timestamp = strtotime($time); // Parses the string into a Unix timestamp
$formatTime = date('H:i', $timestamp); // Outputs '16:25'
echo $formatTime; 

The strtotime() function recognizes standard formats including g:i A (where A represents AM/PM). Once converted to a Unix timestamp, the date() function can format it using the H modifier, which strictly outputs hours in 24-hour notation (00–23), and i for minutes. This approach handles all edge cases automatically, including midnight (12:00 AM becoming 00:00) and noon (12:00 PM becoming 12:00).

If you need to maintain the original input string format while just changing the representation, this method is significantly cleaner than using str_replace. It avoids issues with extra whitespace or inconsistent casing (e.g., "am", "Am", "AM") because strtotime() normalizes these variations internally.

In larger applications using Laravel, you might find yourself working with Carbon, which offers a similar interface but with extended functionality for time zones and relative durations. However, for a simple conversion task, the native PHP functions are sufficient and introduce no overhead. You can also pass the formatted string directly into date() without an intermediate timestamp if you prefer a more functional style:

$time = '09:15 AM';
$twentyFourHourTime = date('H:i', strtotime($time));

This pattern is robust enough to handle inputs like 11:25 AM correctly, converting them to 11:25. The key advantage here is separation of concerns: the parsing logic resides entirely within PHP's core, reducing the surface area for bugs in your business logic.

When implementing this, ensure that the input string does not contain unexpected characters or timezone offsets unless you explicitly handle them. If your source data comes from a specific region where "AM" might be localized differently, you may need to normalize the input first. But for standard English inputs like those in your example, strtotime() is the most reliable tool available.

By leveraging these built-in functions, you avoid reinventing the wheel and ensure that your code remains maintainable regardless of future changes in how time data is presented.

Our network