user3023566
user3023566

Reputation: 169

PHP DateTime Format

How would you change the format of the DATETIME from a MySQL Database.

Code:

echo $r["date"];
echo date("F j, g:ia",$r["date"]);

Output:

2014-02-05 15:31:51
December 31, 6:33pm

These are both two different dates, not sure why.

Upvotes: 1

Views: 1654

Answers (3)

Lajos Veres
Lajos Veres

Reputation: 13725

Try this way:

echo date("F j, g:ia",strtotime($r["date"]));

Mysql returns a string, not a unix timestamp. You should convert it before using with date().

Upvotes: 1

John Conde
John Conde

Reputation: 219894

date() requires the second parameter to be a unix timestamp. You need to pass your datetime string to strtotime() before using it in date()

echo date("F j, g:ia",strtotime($r["date"]));

See it in action

Upvotes: 3

Robbert
Robbert

Reputation: 6592

Use strtotime

echo date("F j, g:ia",strtotime($r["date"]));

The second parameter for the date function requires a timestamp. strtotime converts a date string to a timestamp.

Upvotes: 1

Related Questions