English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hi,

I want to know what format I should store dates in mysql database e.g. 10/09/07?? I need to order the dates when they are retrieved from the database, what date format will mysql understand when I use ORDERBY?

2007-08-12 10:15:10 · 4 answers · asked by jeff lemon 1 in Computers & Internet Programming & Design

4 answers

You should store them as the MySQL DATE or DATETIME data type, DATETIME includes a time as well e.g.

CREATE TABLE [Accounts] (
[account_key] int,
[added_date] DATETIME
)

As for writing the data away and performing comparisons you should use the format 'YYYY-MM-DD HH:MM:SS' e.g.

WHERE [added_date] > '2007-03-07 00:00:00'

This format avoids any ambiguity about which way round day and month values are; US dates are MM/DD/YYYY whereas UK dates are DD/MM/YYYY.

2007-08-12 10:55:02 · answer #1 · answered by Derek F 2 · 1 0

Check DATE function in www.php.net (the official site). All options are there.
When WRITING to DB, convert your date into TIMESTAMP format (just a long number: the number of seconds since January 1 1970 00:00:00). That is what is written in the DB field. The ORDERBY works very well in that case if your field is DATETIME type.
Then, when you READ from DB, the ORDERBY (ASC or DESC) will work fine, and when you display, convert your DATETIME format (long number) into the DATE() format you want...
Easy!

2007-08-13 04:03:39 · answer #2 · answered by just "JR" 7 · 0 0

Mysql stores the date as a number
DATE: A three-byte integer packed as DD + MM×32 + YYYY×16×32,
It may make more sense if you understand the timestamp: A four-byte integer representing seconds UTC since the epoch ('1970-01-01 00:00:00' UTC)
ORDERBY will just sort the "datenumber" as any other number.

2007-08-12 17:49:27 · answer #3 · answered by Doyzer 2 · 0 1

the date is a datatype .. and a data type have rules for compare..

the "date format" is only for string conversion..

2007-08-12 17:26:49 · answer #4 · answered by Anonymous · 0 1

fedest.com, questions and answers