1

I'm working on a small self made blog and I insert new blog items into phpmyadmin manually. On the website I run a query to select the blog items.

I want dates to be displayed as:

April 30th, 2011

Since I already insert the items myself, would it be better to just have a VARCHAR field and insert the date mysql, or is there a better way?

I am not sure which field type to use in phpmyadmin and also don't really know how to use PHP to get the date to be displayed the way I want it to be.

Thanks!

2 Answers 2

4

Use the MySQL datetime field, not a varchar field. That way you could sort by date and/or do other nice things. It also allows you to later change the way dates are displayed.

Sign up to request clarification or add additional context in comments.

4 Comments

Alright thank you for your quick answer. I've set the field to 'datetime' and it's displayed like this in the database: 2011-04-30 17:50:20 Now how do I convert it into the date I want it to be?
+1 And deal with regional settings too eg 01.05.20011, 01 Mai 2011, May 1st, 2011 etc
Not really sure what you meant on regional settings. How does that show me the date that I want it be displayed?
You can use the php function strtotime to convert the date from your database to a unix timestamp and then use strftime to format it.
1

I'd not insert the date myself if it's for blog entries or comments. You can use MySQL's timestamp which by default uses current timestamp if you don't provide anything. This way you can prepare your blog for situation when you'll use some admin section for new articles and for now you'll save yourself from typing several characters.

As for formatting it for output, you can use DateTime class and format it as follows:

$date = date_create('2011-04-30 17:50:20');
echo date_format($date, 'F jS, Y');

or OOP way:

$date = new DateTime( '2011-04-30 17:50:20' );
echo $date->format( 'F jS, Y' );

1 Comment

Thank you, but now I still do not know how I get the database date: 2011-04-30 17:50:20 To be shown as April 30th, 2011 on my page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.