Page 1 of 1
Ugh! Just want to sort date
Posted: Mon Apr 17, 2006 5:04 pm
by xterra
I realized that when using plain text datatype fields it can't sort the date. However, when I set it to the DateTime field, my date has to be formatted like this:
0000-00-00 00:00:00
That's fine with me. I just need it to work since Im making a forum and it needs to be sorted by new posts. So I figured to get it to that format I made this code:
$date=date('Y-m-d G:i:s');
But it will not go in the database? Can someone show me how to correctly make the date and time, put it in the database but allow it to be sorted later?
Thanks.
Posted: Mon Apr 17, 2006 5:14 pm
by hawleyjr
Store it as a normal date time field and use mysql date functions to format it
http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html
Posted: Mon Apr 17, 2006 5:19 pm
by s.dot
or just use a unix timestamp to sort, and use date() to print the date in a readable format
Posted: Mon Apr 17, 2006 6:08 pm
by printf
I would just store in the database like hawleyjr said, then use the database when selecting to format the date and time in your SELECT query. Less coding is always better!
pif!
Posted: Mon Apr 17, 2006 6:12 pm
by shiznatix
ill toss down my 2 cents and agree with scottayy. if you store it in unix timestamp it is very easy to order it in a SQL statement and you can always change the layout of the date using the date() function. Different places in the world show the time and the date in different formats and you should be able to show it to them accordingly.
Re: Ugh! Just want to sort date
Posted: Mon Apr 17, 2006 6:16 pm
by RobertGonzalez
xterra wrote:That's fine with me. I just need it to work since Im making a forum and it needs to be sorted by new posts.
Why not sort by post ID instead of date/time? Post ID should be as accurate of a sort field as a date/time field and a lot easier to query/sort by.
As for the time issue, I tend to go with what all the other posters are saying. Use the MySQL UNIX_TIMESTAMP() function or use the
strtotime() PHP function along with the
date() function.
Posted: Mon Apr 17, 2006 9:42 pm
by d3ad1ysp0rk
All you need to do is use:
INSERT INTO posts(title,body,date) VALUES('$title','$body','NOW()');
Posted: Tue Apr 18, 2006 12:01 pm
by timvw
NOW() shouldn't be quoted
