How MySQL Handles Dates

Posted on March 15, 2009

MySQL stores dates in the format YYYYMMDD, ie year, month, day. This is in acordance with the ISO (International Organization for Standardisation.

Dates and time in MySQL always follow the same order, largest unit first, then next largest, and so on to the smallest unit.

So dates are in the order year,month,date and

time is in the order hours,minutes,seconds

Hours are measured using the 24 hour clock with midnight at 00:00:00 and 12 noon as 12:00:00.

MySQL has a number of useful functions that can be used to manipulate dates. These include:

  • DATE_FORMAT()  - formats a date
  • DATE_ADD() - adds dates
  • DATE_SUB() - subtracts dates

For details of how these functions work you can get detailed instructions at the MySQL Reference On Date And Time Functions

Here is a simple example:

Example of DATE_FORMAT() usage

The syntax for DATE_FORMAT() is:

DATE_FORMAT(date, format)

date is the table column to be formatted and

format is the format to use

So

DATE_FORMAT(updated, ‘%e/%c/%Y’) As updated

would format the dates in the updated column in european date style.

%e - means without leading zero  (this applies to the day)

%c - means without leading zero (this applies to the month)

%Y - means use four digit format (this applies to the year)

An example would be 25/4/2009

Posts You May Also Be Interested In:

  • No Related Post

Tags: , , , ,

1 Response

  1. Gerard
    January 28, 2010

    Here is a tool to help you format dates using the date_format function http://www.mysqlformatdate.com


Leave a Reply