Forum Moderators: coopster

Message Too Old, No Replies

MySQL date format

         

bysonary

8:49 pm on May 28, 2007 (gmt 0)

10+ Year Member



I am in the process of thinking about doing something but have kinda hit a mental brick wall.

I want to be able to insert stuff into a database, thats fine yeah, but how would I go about sorting it by the date it was submitted?

I am in the UK where the date format is DD/MM/YYYY and not MM/DD/YYYY, I know that mysql takes the date in the US format of MM/DD/YYYY, is there anyway to change this and successfully sort the data by the date it was submitted, e.g. have the oldest at the bottom and newest at the top?

any ideas, help or references would be appreciated.

Thanks

Chris

jatar_k

9:24 pm on May 28, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



just store a timestamp, you can then sort easily and can format it however you like for display

camilord

9:25 pm on May 28, 2007 (gmt 0)

10+ Year Member



yeah.. i usually use mktime() hehehee.. :)

phranque

9:30 pm on May 28, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



this is the mysql reference on date and time types [dev.mysql.com].
this is the mysql reference on date and time functions [dev.mysql.com].
with these functions you can accomplish just about any time slicing and formatting you could imagine.
the storage format of all date and time types makes them naturally sortable.

for example:
SELECT stuff, DATE_FORMAT(submitted_date, '%d/%m/%Y') AS uk_date FROM mytable ORDER BY submitted_date DESC;

note: as of mysql 5.1 there are some changes you should be aware of regarding language, character set and collation sequence for date_format() results.

phranque

9:36 pm on May 28, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



jatar_k:
just store a timestamp, you can then sort easily and can format it however you like for display

the timestamp column will typically update whenever an insert or update occurs.
this is the easy way to track the last change.
to specifically control the submitted date, you should use a datetime column and mktime() function on insert as camilord pointed out.

jatar_k

10:01 pm on May 28, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ah, I didn't say make a TIMESTAMP column, I said "store a timestamp"

I never use TIMESTAMP columns

phranque

11:17 pm on May 28, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



ah, I didn't say make a TIMESTAMP column, I said "store a timestamp"

my bad.
i would have called that a "time stamp".

bysonary

12:49 am on May 29, 2007 (gmt 0)

10+ Year Member



Nice cheers for all the suggestions, bit of readin for me, thanks again webmasterworld and your members.