If I have a date in this format in my mysql db:

Y-m-d H:i:s

Once it’s pulled out and ready to be displayed on a page, how can I get it to convert that into a nicer, easier format to read ie:

Sunday 12th December 5:52pm



You could use the date_format function

echo date_format($date, 'yourvalues');

I have just tried the following and get errors:

<p><?php date_format($news['datetime'], 'l ts F Y'); ?></p>

$news[‘datetime’] is a date in the format I posted in my OP.

what are the errors you’re getting?

Error is:

Warning: date_format() expects parameter 1 to be DateTime, string given in D:\Files\wamp\www
eilnews\show_news.php on line 14

Line 14 is:

<h3><?php echo $news['subject']; ?></h3><p><?php echo $news['article']; ?></p><p><?php date_format($news['datetime'], 'l ts F Y'); ?></p>

$date = '2010-12-25 00:00:00';
echo date('l jS F Y', strtotime($date));
#Saturday 25th December 2010

That’s great. Can you just explain how it is doing what it does?

Sure, [fphp]strtotime[/fphp] converts the string $date into a UNIX timestamp and then [fphp]date[/fphp] formats said timestamp.

Provided the date is coming off a mysql date or datetime field it is faster to use MySQL’s dateformat() function than use strtotime/date. Example:

SELECT DATE_FORMAT(datefield, '%W %D %M %l:%i%p') AS formattedDate FROM myTable

faster in what way?

in performance? i remain unconvinced until i see actual and reliable cpu timings

to code? again, i doubt it, as a good programmer can do it either way real fast

the real test, in my opinion, is in maintainability

most experienced develoipers will tell you that the job of the database is retrieval, and formatting should be done in the front end application

this becomes painfully clear if you’ve ever worked in an organization where changes to working queries have to be vetted and approved by a separate part of the systems department, whereas changes to the app proceed without this bottleneck

(at least, that’s what i used to tell developers when i worked in the bottleneck, er, i mean, database section)