I'm looking for a way to convert army time (such as 23:00) to regular time with the AM/PM expression.
Asked
Active
Viewed 1.8k times
6
-
4I 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
19
Just pass your time in strtotime function like this:
$time_in_12_hour_format = date("g:i a", strtotime("23:00"));
echo $time_in_12_hour_format;
Pier-Luc Gendreau
- 12,875
- 4
- 58
- 64
sharif2008
- 2,610
- 2
- 18
- 33
8
http://www.php.net/manual/en/function.date.php
$army_time_str = "23:00";
$regular_time_str = date( 'g:i A', strtotime( $army_time_str ) );
echo $regular_time_str;
Scott Mutch
- 146
- 1
- 3
3
input $time is in the form of 'XXXX' (e.g. '0000', '0001',...,'2300'). i couldn't find a function for this so wrote the following. wanted to post here in case anyone else is looking for the same thing.
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;
}
tbradley22
- 1,495
- 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