How to Convert Military Time to Standard Time in PHP

Sheeraz Gul Feb 02, 2024
  1. Use the date() Method to Convert Military Time to Standard Time in PHP
  2. Use the DateTime Object to Convert Military Time to Standard Time in PHP
How to Convert Military Time to Standard Time in PHP

This tutorial demonstrates how to convert military time to standard time in PHP.

The 24-hour time is considered military time, for example, 22:10, and the 12-hour time with AM/PM is considered the standard time. The PHP provides two methods to convert military time to standard time.

Use the date() Method to Convert Military Time to Standard Time in PHP

The PHP built-in function date() can convert military time to standard time in PHP. We also need to use the strtotime() method to convert the time string to time type.

Let’s see the example:

<?php
//H:i:s format date string in military time.
$Demo_Date = '19:36:09';


//standard format with uppercase AM/PM
echo "The time in standard format is: ".date("g:iA", strtotime($Demo_Date ));
?>

The code above converts the time given in a military format to the standard format using the uppercase AM/PM. The character g in the date() function represents the 12-hour format, and the character A is for the uppercase AM/PM.

Let’s see the output:

The time in standard format is: 7:36PM

We can also get the time in standard format with lowercase am/pm by converting the character A to a. See the example:

<?php
//H:i:s format date string in military time.
$Demo_Date = '19:36:09';


//standard format with lowercase am/pm
echo "The time in standard format is: ".date("g:ia", strtotime($Demo_Date ));
?>

The code above will convert the military time to standard time using the lowercase am/pm. See the output:

The time in standard format is: 7:36pm

Use the DateTime Object to Convert Military Time to Standard Time in PHP

We can also use the DateTime object of PHP to convert the military time to standard time in PHP. We use the same format, g:iA or g:ia, but this time on the object.

Let’s try an example:

<?php
//H:i:s format date string in military time.
$Demo_DateTime = new DateTime('19:36:09');

//standard format with uppercase AM/PM
echo "The time in standard format is: ".$Demo_DateTime->format('g:iA');
?>

The code above will convert the military time to standard time with uppercase AM/PM. See output:

The time in standard format is: 7:36PM

Similarly, for lowercase am/pm:

<?php
//H:i:s format date string in military time.
$Demo_DateTime = new DateTime('19:36:09');

//standard format with lowercase am/pm
echo "The time in standard format is: ".$Demo_DateTime->format('g:ia');
?>

The code above will convert the military time to standard time with lowercase am/pm. See output:

The time in standard format is: 7:36pm

If you want to show the time with a leading zero, you should use h instead of g in the format. See the example:

<?php
//H:i:s format date string in military time.
$Demo_DateTime = new DateTime('19:36:09');

//standard format with uppercase AM/PM
echo "The time in standard format is: ".$Demo_DateTime->format('h:iA')."<br>";

//standard format with lowercase am/pm
echo "The time in standard format is: ".$Demo_DateTime->format('h:ia');
?>

The output for this format is:

The time in standard format is: 07:36PM
The time in standard format is: 07:36pm
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - PHP Time