user3109875
user3109875

Reputation: 828

How to echo a converted time format from mysql datetime column

I'm trying to convert datetimte to something shorter, but i get some error as below.

Warning: date_format() expects parameter 1 to be DateTime, string given in ...

CODE

$datetime = '2012-03-24 17:45:12';
$time = date_format($datetime, 'g:i A');
echo $time;

What i'm i doing wring? Thanks.

Upvotes: 0

Views: 183

Answers (3)

steven
steven

Reputation: 646

date_format formats a date object. You have a strong.

Try date("g:I A", strtotime($datetime))

Upvotes: 0

Fazal Rasel
Fazal Rasel

Reputation: 4526

this will also work for you-

$datetime = date_create('2012-03-24 17:45:12');
$time = date_format($datetime, 'g:i A');
echo $time; 

Upvotes: 0

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44874

Try this with DateTime

$date = new DateTime("2012-03-24 17:45:12");
echo $date->format("g:i A");

Upvotes: 2

Related Questions