Jump to content

Date formatting help


fife

Recommended Posts

Hi Guys

 

I am now heading to a section in my website were is will be using dates.  I'll be comparing, adding and subtracting dates from each other.  I am just wondering if anybody know of any decent tutorials out there to best describe how to store dates in a database which will best suit this purpose.  At the moment I am storing them in the English format of dd/mm/yyyy.  I have read a few articles but lots of people have different ideas on how this is done.  Im looking for the more professional approach.

 

Thanks

Link to comment
Share on other sites

htmlGo to MySQL Reference - 10.5. Data Type Storage Requirements

http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.

Search for: Storage Requirements for Date and Time Types

 

Dates are internally stored as A three-byte integer packed as DD + MM×32 + YYYY×16×32

 

But, if you select a date column for display, it has to be shown in some way, so it comes out as 0000-00-00. It is not stored as a char(10) with that specific format.

 

If, for display, you need to see a specific format, you can convert it to VARCHAR in a specific format using Date_Format(). However, bear in mind that if you are using the query in a programming tool, this is not what you want to do. You want the raw date value, for display purposes, there will be a similar formatting function from whatever programming environment you use.

 

As you can see from the reference in DATE_FORMAT, you will want to use '%d-%m-%Y', e.g.

 

SELECT col1, col2, DATE_FORMAT(datecolumn, '%d-%m-%Y') AS datecolumn, more1... FROM sometable ....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.