45808
45808

Reputation: 376

SQL and PHP date

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!

Upvotes: 1

Views: 608

Answers (2)

Ondrej Slinták
Ondrej Slinták

Reputation: 31910

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' );

Upvotes: 1

Arjan
Arjan

Reputation: 9884

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.

Upvotes: 4

Related Questions