Date formats
The DATE_FORMAT function is a powerful function that allows you to return a specified date in a number of different ways. For example, those who use American time (MM-DD-YYYY, as you can see at the beginning of this article) instead of the standard International time, this function provides a painless way to convert. Below is a list of format specifiers:
| %a | Abbreviation of the day (from Sun-Sat) |
| %b | Abbreviation of the month (from Jan-Dec) |
| %c | Numeric month (from 1-12) |
| %D | Numeric day of the month with suffix (1st, 2nd, and so on) |
| %d | Numeric day of the month with two digits(from 00-31) |
| %e | Numeric day of the month with one or two digits(from 0-31) |
| %H | Hour (from 00-23) |
| %h | Hour (from 01-12) |
| %i | Minutes (from 00-59) |
| %I | Hour (from 01-12) |
| %j | Day of the year (from 001-366) |
| %k | Hour with one or two digits (from 0-23) |
| %l | Hour with one digit (from 1-12) |
| %M | Month name (from January-December) |
| %m | Numeric month (from 01-12) |
| %p | A.M. or P.M. |
| %r | 12-hour time (hh:mm:ss A.M.or P.M.) |
| %S | Seconds (from 00-59) |
| %s | Seconds (from 00-59) |
| %T | 24 hour time (hh:mm:ss) |
| %U | Week (from 00-53, Sunday being the first day of the week) |
| %u | Week (from 00-53, Monday being the first day of the week) |
| %V | Week (from 01-53, Sunday being the first day of the week) |
| %v | Week (from 01-53, Monday being the first day of the week) |
| %W | Name of the day in the week (from Sunday-Saturday) |
| %w | Day of the week (from 0 - Sunday, to 6 - Saturday) |
| %X | Four-digit numeric year for the week (Sunday being the first day of the week) |
| %x | Four-digit numeric year for the week (Monday being the first day of the week) |
| %Y | Four-digit numeric year |
| %y | Two-digit numeric year |
| %% | Percentage sign (escaped) |
Let's look at converting a standard date into a format used in the US.
mysql> SELECT DATE_FORMAT('2003-07-14','%b %d,%Y');
+--------------------------------------+
| DATE_FORMAT('2003-07-14','%b %d,%Y') |
+--------------------------------------+
| Jul 14,2003 |
+--------------------------------------+
A subset of this function is the TIME_FORMAT() function, which is identical, but allows you to use those formats to do with time.
The last function we are going to look at specifically will be meaningful to those of you familiar with the concept of Unix time. Unix time is the time in seconds since midnight 1 January 1970, and is used by many applications. The UNIX_TIMESTAMP() function returns the Unix time of the current time When called without a parameter, or converts a specified date if one is supplied.
mysql> SELECT UNIX_TIMESTAMP();
+------------------+
| UNIX_TIMESTAMP() |
+------------------+
| 1050267998 |
+------------------+
mysql> SELECT UNIX_TIMESTAMP('2003-07-14');
+------------------------------+
| UNIX_TIMESTAMP('2003-07-14') |
+------------------------------+
| 1058133600 |
+------------------------------+
Looking at the possibilities available with this function, along with the date and time calculations we've looked at already, you can see there's not always that much need to do this inside the application!
Below is a reference for the MySQL date and time functions. Once you have mastered what we've covered so far, none of the other functions will present anything tricky, but you should give the list a read through - you never know when you'll need them.