6

Is there a way to convert army time (such as 23:00) to regular time with the AM/PM expression?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tbradley22
  • 1,535
  • 2
  • 15
  • 14

3 Answers3

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
  • 1
    It'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
  • 3
    The 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