How do I convert datetime field to readable format?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I convert datetime field to readable format?

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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');
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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?!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

Take a look at the date page. Has all the formatting strings listed. Sunday would be l (lowercase L, not uppercase i)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

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

Post by simonmlewis »

Thanks will look. But yes I did pregmatch. Just the job!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply