Page 1 of 1

How do I convert datetime field to readable format?

Posted: Sat May 03, 2014 7:27 am
by simonmlewis
The field type is "datetime".
An example of it is:
[text]2014-05-02 22:20:18[/text]
How do I convert that, to something like: 2 May 2014, 22:20:18 ?

I thought this would work:

Code: Select all

echo date("D, d M Y H:i:s T",$row->post_date);
But it just defaults to the Jan 1970 output.

Re: How do I convert datetime field to readable format?

Posted: Sat May 03, 2014 7:31 am
by Celauran
date() takes a timestamp as the second argument.

Code: Select all

echo date('D, d M Y H:i:s T', strtotime($row->post_date));
Alternately, use DateTime.

Code: Select all

$dt = new DateTime($row->post_date);
echo $dt->format('D, d M Y H:i:s T');

Re: How do I convert datetime field to readable format?

Posted: Sat May 03, 2014 7:45 am
by simonmlewis
Brilliant. Looks like I just missed the strtotime part.
How can I set the word Saturday to show in full? I tried DD but you will know the result of that?!

Re: How do I convert datetime field to readable format?

Posted: Sat May 03, 2014 8:08 am
by simonmlewis
Do you happen to know if there is anyway in Word Press to identify in MySQL code, if a post has an image attached?
I think the only means of doing is it querying if "<img" is written in the code. As I don't think it's entered into a separate field.

Just be cool to know, so I can add a 'paperclip' icon to the feed to our blog.

Re: How do I convert datetime field to readable format?

Posted: Sat May 03, 2014 8:17 am
by Celauran
Take a look at the date page. Has all the formatting strings listed. Sunday would be l (lowercase L, not uppercase i)

Re: How do I convert datetime field to readable format?

Posted: Sat May 03, 2014 8:19 am
by Celauran
simonmlewis wrote:Do you happen to know if there is anyway in Word Press to identify in MySQL code, if a post has an image attached?
You don't mean has_post_thumbnail, do you? Rather an inline image? I don't know of anything built in, but you could do a preg_match on <img in the post itself.

Re: How do I convert datetime field to readable format?

Posted: Sat May 03, 2014 8:22 am
by simonmlewis
Thanks will look. But yes I did pregmatch. Just the job!