(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.. "
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
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 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.