A PHP/MySQL question - geeks only need apply: how to get a formatted date from an associative array
(He writes, noting all the comments piling up about PKRSER.COM.. hmm…)
I’ve got an intriguing PHP problem. Well, I hope it’s intriguing. I’m trying to format a date that comes out of MySQL. Presently I have to do it as
"SELECT ID, post_title, post_date,MATCH (post_name, post_content) AGAINST ('$terms') AS score.. "
That produces post_date which is in the MySQL datetime format - 2007-03-08 07:55:37.
But I want to put that in the form of “8 March 2007″. Yes, I know, you’re saying “Use MySQL’s date_format command!” Not so fast. The query produces an associative array called (surprise!) $results, where the results are then called one by one in a foreach loop (foreach $results as $result) thus:
$title = htmlspecialchars(stripslashes($result->post_title));
$permalink = get_permalink($result->ID);
See? You asked it for ID and post_title and it comes directly out of $result. Trouble is, post_date comes out in that boring “2007-03-08 07:55:37″ format.
Now, MySQL’s date_format could be used: SELECT ID, post_title, date_format(post_date, "%d %M %Y"),MATCH (post_name, post_content)...
And that would yield the post date nicely formatted. But how the hell do I call it?
$published = $result->date_format fails.
$published = $result[2] fails (which I found odd.)
How do I call the variable created by MySQL’s date formatting? Or am I doomed to use the rather boring substr on the boring date?
(I don’t really want to go off into parsing the months of the date, just to make the pages run quicker.)
You might be able to guess what this is for, though it requires some low-level tweaking too.
The original I’m working with is the tweaked Nice Related Posts plugin for Wordpress, which I’m tweaking further. If you want, I can release the code, assuming I’m ever done.
(And you have to give its author kudos for the domain name. “Some fool with a .com”. Yeah.)
Update: solved - tip o’ the hat to Jason and L in the comments, who point out that one can use the SQL command SELECT date_format(post_date,"%e %M %Y") as publ_date… and then call $result->publ_date which will be formatted as 8 March 2007 (in that setting). Thank you, the lazyweb.
- These posts might be related (the database thinks..):
- 'Appcasting' - keep your software up to date via RSS enclosures (12 May 2005; score: 51.04%)
- Uh? The freebie flash memory stick that's read-only (20 March 2005; score: 34.74%)
- Apple vs Windows: 'Palestine for geeks' (15 June 2007; score: 34.53%)



