93

I have a date time in a variable. My format is 08/04/2010 22:15:00. I want to display this like 10.15 PM. How to do this in PHP?

jkdev
  • 11,360
  • 15
  • 54
  • 77
Testadmin
  • 2,880
  • 9
  • 49
  • 71
  • Similar: [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:21

12 Answers12

204

You need to convert it to a UNIX timestamp (using strtotime) and then back into the format you require using the date function.

For example:

$currentDateTime = '08/04/2010 22:15:00';
$newDateTime = date('h:i A', strtotime($currentDateTime));
John Parker
  • 54,048
  • 11
  • 129
  • 129
26
$dateString = '08/04/2010 22:15:00';
$dateObject = new DateTime($dateString);
echo $dateObject->format('h:i A');
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
20

Use strtotime() to make the date a UNIX timestamp.

For output, check out the various options of date().

$timestamp = strtotime("08/04/2010 22:15:00");
date("h.i A", $timestamp);
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 2
    Not too sure about the uppercase "H" in the date call (odd to have a 24hr with the AM/PM). Yes, I'm quibbling as you've beaten me to it. Again. :-) – John Parker Aug 04 '10 at 10:45
15
<?php 
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata')); 
echo $dateTime->format("d/m/y  H:i A"); 
?>

You can use this to display the date like this

22/06/15 10:46 AM
phpmaster
  • 159
  • 1
  • 2
11

Like this:

$date = '08/04/2010 22:15:00';
echo date('h:i A', strtotime($date));

Result:

10:15 PM

More Info:

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
7

for flexibility with different formats, use:

$dt = DateTime::createFromFormat('m/d/Y H:i:s', '08/04/2010 22:15:00');
echo $dt->format('g:i A')

Check the php manual for additional format options.

deck
  • 880
  • 10
  • 10
7

PHP Code:

date_default_timezone_set('Asia/Kolkata');
$currentDateTime=date('m/d/Y H:i:s');
$newDateTime = date('h:i A', strtotime($currentDateTime));
echo $newDateTime; 

Output: 05:03 PM

rabin
  • 71
  • 1
  • 1
6
$currentDateTime = $row['date'];
echo $newDateTime = date('l jS \of F Y h:i:s A', strtotime($currentDateTime));
imtaher
  • 430
  • 4
  • 9
3

Perfect answer for AM/PM live time solution

<?php echo date('h:i A', time())?>
Amit Ghosh Anto
  • 141
  • 1
  • 4
3

Just simply right A

{{ date('h:i A', strtotime($varname->created_at))}}
Amit Verma
  • 2,427
  • 2
  • 7
  • 6
1

For (PHP >= 5.2.0):

You can use DateTime class. However you might need to change your date format. Didn't try yours. The following date format will work for sure: YYYY-MM-DD HH-MM-SS

$date = new DateTime("2010-04-08 22:15:00");
echo $date->format("g"). '.' .$date->format("i"). ' ' .$date->format("A");

//output
//10.15 PM

However, in my opinion, using . as a separator for 10.15 is not recommended because your users might be confused either this is a decimal number or time format. The most common way is to use 10:15 PM

Andrei Stalbe
  • 1,511
  • 6
  • 26
  • 44
0

It is quite easy. Assuming you have a field(dateposted) with the type "timestamp" in your database table already queried and you want to display it, have it formated and also have the AM/PM, all you need do is shown below.

     <?php
     echo date("F j, Y h:m:s A" ,strtotime($row_rshearing['dateposted'])); 


    ?>

Note: Your OUTPUT should look some what like this depending on the date posted

May 21, 2014 03:05:27 PM

Okwo moses
  • 85
  • 6