Is there a way to convert army time (such as 23:00) to regular time with the AM/PM expression?
Asked
Active
Viewed 1.9k times
6
-
5I think "Army time" is more popularly known as the "[24 hour clock](https://en.wikipedia.org/wiki/24-hour_clock)" `:-p` – halfer Jun 10 '13 at 22:34
-
1$time24 = '23:00'; $dt = new DateTime($time24); echo $dt->format('h:i A'); – Mark Baker Jun 10 '13 at 22:37
-
3@marabutt - I think you have to have a minimum of 1K rep here before you're allowed to swear at posters. – halfer Jun 10 '13 at 22:39
-
Duplicate of: [How to convert the time from AM/PM to 24 hour format in PHP?](http://stackoverflow.com/q/16955209/55075) – kenorb Nov 14 '15 at 00:22
-
try this from loop: echo date("g:i a", strtotime($result['hourOpeningHour'])) – Amranur Rahman Dec 04 '17 at 13:10
3 Answers
20
Just pass your time to the strtotime function like this:
$time_in_12_hour_format = date("g:i a", strtotime("23:00"));
echo $time_in_12_hour_format;

Peter Mortensen
- 30,738
- 21
- 105
- 131

sharif2008
- 2,716
- 3
- 20
- 34
8
Use date:
$army_time_str = "23:00";
$regular_time_str = date( 'g:i A', strtotime( $army_time_str ) );
echo $regular_time_str;

Peter Mortensen
- 30,738
- 21
- 105
- 131

Scott Mutch
- 146
- 1
- 3
3
The input $time is in the form of 'XXXX' (e.g. '0000', '0001',...,'2300'). I couldn't find a function for this, so I wrote the following.
function convert_army_to_regular($time) {
$hours = substr($time, 0, 2);
$minutes = substr($time, 2, 2);
if ($hours > 12) {
$hours = $hours - 12;
$ampm = 'PM';
} else {
if ($hours != 11) {
$hours = substr($hours, 1, 1);
}
$ampm = 'AM';
}
return $hours . ':' . $minutes . $ampm;
}

Peter Mortensen
- 30,738
- 21
- 105
- 131

tbradley22
- 1,535
- 2
- 15
- 14
-
1It's probably better to convert the time to a unix timestamp, and then render using `date` - which is very flexible. Still, +1. – halfer Jun 10 '13 at 22:37
-
3The obvious answer is to convert it to a unix timestamp or a DateTime object, and then use date() or the DateTime format() method... so much cleaner and easier – Mark Baker Jun 10 '13 at 22:38