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 do开发者_Python百科n't really know how to use PHP to get the date to be displayed the way I want it to be.
Thanks!
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.
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' );
精彩评论