rctneil — 2010-12-12T12:52:31-05:00 — #1
If I have a date in this format in my mysql db:
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
donboe — 2010-12-12T13:01:08-05:00 — #2
You could use the date_format function
echo date_format($date, 'yourvalues');
rctneil — 2010-12-12T13:09:03-05:00 — #3
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.
donboe — 2010-12-12T13:34:02-05:00 — #4
what are the errors you're getting?
rctneil — 2010-12-12T13:36:26-05:00 — #5
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>
anthonysterling — 2010-12-12T13:41:01-05:00 — #6
$date = '2010-12-25 00:00:00';
echo date('l jS F Y', strtotime($date));
#Saturday 25th December 2010
rctneil — 2010-12-12T13:43:47-05:00 — #7
That's great. Can you just explain how it is doing what it does?
anthonysterling — 2010-12-12T13:50:52-05:00 — #8
Sure, strtotime converts the string $date into a UNIX timestamp and then date formats said timestamp.
michael_morris1 — 2010-12-12T13:56:36-05:00 — #9
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
r937 — 2010-12-12T18:26:55-05:00 — #10
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)