Convert time to am pm php

Asked 9 years, 3 months ago

Viewed 201k times

From a data feed I'm getting date and time in this format:

19:24:15 06/13/2013

I need to have it converted to be in 12-hour AM/PM without the seconds. So the above time would be 7:24 PM. The date should remain in mm/dd/yyyy format. Is there an elegant way to do this in PHP (not MySQL)? Thanks!

Convert time to am pm php

asked Jun 13, 2013 at 22:20

6

I think you can use date() function to achive this

$date = '19:24:15 06/13/2013'; 
echo date('h:i:s a m/d/Y', strtotime($date));

This will output

07:24:15 pm 06/13/2013

Live Sample

h is used for 12 digit time
i stands for minutes
s seconds
a will return am or pm (use in uppercase for AM PM)
m is used for months with digits
d is used for days in digit
Y uppercase is used for 4 digit year (use it lowercase for two digit)

Updated

This is with DateTime

$date = new DateTime('19:24:15 06/13/2013');
echo $date->format('h:i:s a m/d/Y') ;

Live Sample

answered Jun 13, 2013 at 22:24

Convert time to am pm php

FabioFabio

22.7k12 gold badges52 silver badges63 bronze badges

2

You can use date function to format it by using the code below:

echo date("g:i a", strtotime("13:30:30 UTC"));

output: 1:30 pm

Max Gaurav

1,7552 gold badges12 silver badges25 bronze badges

answered Apr 5, 2018 at 10:33

AowalAowal

1211 silver badge2 bronze badges

Use smaller h

// 24 hrs 
H:i 
// output 14:20

// 12 hrs 
h:i 
// output 2:20

answered Jul 25, 2018 at 9:46

Convert time to am pm php

DexterDexter

6,4723 gold badges35 silver badges34 bronze badges

This is a PHP tutorial on how to convert a standard 24 hour date and time into a 12 hour time. Furthermore, I will also show you how to add the AM and PM period abbreviations.

Take the following example:

//Example Y-m-d H:i:s date string.
$date = '2019-02-15 16:56:01';

//12 hour format with uppercase AM/PM
echo date("g:iA", strtotime($date));

In the code above, we converted a standard datetime string into a 12 hour time with an uppercase AM/PM abbreviation. We did this by using PHP’s inbuilt date and strtotime functions. The character “g” tells the date function that we want a 12-hour format of an hour without leading zeros. The uppercase “A” character is for AM / PM.

But what if you want a lowercase am / pm abbreviation?

//Example Y-m-d H:i:s date string.
$date = '2019-02-15 16:56:01';

//12 hour format with lowercase AM/PM
echo date("g:ia", strtotime($date));

As you can see, it’s pretty much the same as the first example. The only difference is that we used a lowercase a instead of an uppercase A in the format parameter.

Note that we removed the time in seconds from both examples as seconds are usually not typically displayed in this format.

Using the DateTime object.

If you prefer using PHP’s DateTime object, then you can use the following code:

//Create a DateTime object using the original
//datetime string.
$date = new DateTime('2019-02-15 16:56:01');

//Convert it into the 12 hour time using the format method.
echo $date->format('g:ia') ;

The above piece of code will print out: 4:56pm

Leading zeros.

If you do want to display leading zeros in your time format, then you should use the character h instead of g.

An example:

//Create a DateTime object using the original
//datetime string.
$date = new DateTime('2019-01-14 09:12:10');

//Print out the 12 hour time using the format method.
echo $date->format('h:ia') ;

If you run the PHP code above, you should see that it prints out: 09:12am

That’s pretty much all there is to it! Hopefully, this guide solved your problem!

How to get time with AM and PM in PHP?

Use strtotime() to make the date a UNIX timestamp. For output, check out the various options of date(). Not too sure about the uppercase "H" in the date call (odd to have a 24hr with the AM/PM).

How to change time format in PHP?

To convert the date-time format PHP provides strtotime() and date() function..
$date = "06/13/2019 5:35 PM";.
//converts date and time to seconds..
$sec = strtotime($date);.
//converts seconds into a specific format..

How can change time in 24 hour format in PHP?

$time = '23:45'; echo date('g:i a', strtotime($time));

How to show time in 12 hour format in PHP?

php //current Date, i.e. 2013-08-01 echo date("Y-m-d"); //current Time in 12 hour format, i.e. 08:50:55pm echo date("h:i:sa"); //current Time in 24 hour format, i.e. 18:00:23 echo date("H:i:s"); //current Month, i.e. 08 echo date("m"); //current Month name, i.e. Aug echo date("M"); ?>