tags:

views:

38

answers:

4

Hi 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? Does any one help me?

Thanks in advance

+3  A: 

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
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. :-)
middaparka
@middaparka cheers! :) You are totally right. Corrected.
Pekka
A: 

Like this:

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

Result:

10:15 PM

More Info:

Sarfraz
+1  A: 

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));
middaparka
+1  A: 
$dateString = '08/04/2010 22:15:00';
$dateObject = new DateTime($dateString);
echo $dateObject->format('h:i A');
Mark Baker