SECOND | ss |
MINUTE | mm |
HOUR | hh |
DAY | DD |
MONTH | MM |
YEAR | YY |
MINUTE_SECOND | mm:ss |
HOUR_MINUTE | hh:mm |
DAY_HOUR | DD:hh |
YEAR_MONTH | YYYY-MM |
HOUR_SECOND | hh:ss |
DAY_MINUTE | DD hh:mm |
DAY_SECOND | DD hh:mm:ss |
DATE_ADD(datetime, INTERVAL expression datetimetype)
For example, to find a date 14 days after the 13th July, 2003, you can use:
mysql> SELECT DATE_ADD('2003-07-13', INTERVAL 14 DAY); +-----------------------------------------+ | DATE_ADD('2003-07-13', INTERVAL 14 DAY) | +-----------------------------------------+ | 2003-07-27 | +-----------------------------------------+
mysql> SELECT DATE_ADD('2003-07-13 01:01:01', INTERVAL -'22:14' HOUR_MINUTE);
+----------------------------------------------------------------+
| DATE_ADD('2003-07-13 01:01:01', INTERVAL -'22:14' HOUR_MINUTE) |
+----------------------------------------------------------------+
| 2003-07-13 00:39:01 |
+----------------------------------------------------------------+
mysql> SELECT DATE_ADD('2003-07-13', INTERVAL -1 MINUTE);
+--------------------------------------------+
| DATE_ADD('2003-07-13', INTERVAL -1 MINUTE) |
+--------------------------------------------+
| 2003-07-12 23:59:00 |
+--------------------------------------------+
mysql> SELECT DATE_ADD('2003-07-13', INTERVAL '-22:14' HOUR_MINUTE);
+-------------------------------------------------------+
| DATE_ADD('2003-07-13', INTERVAL '-22:14' HOUR_MINUTE) |
+-------------------------------------------------------+
| 2003-07-12 01:46:00 |
+-------------------------------------------------------+
mysql> SELECT DATE_ADD('2003-07-14', INTERVAL -'22:14' HOUR_MINUTE);
+-------------------------------------------------------+
| DATE_ADD('2003-07-14', INTERVAL -'22:14' HOUR_MINUTE) |
+-------------------------------------------------------+
| 2003-07-13 23:38:00 |
+-------------------------------------------------------+
There’s an alternative to using a negative number with the DATE_ADD() function – you could simply use DATE_SUB(), or its synonym SUBDATE(). There is also an alternative if you’re only worried about the YEAR and MONTH components of the date. You can use the PERIOD_ADD() and PERIOD_DIFF() functions. PERIOD_ADD takes a period (specified as YYYYMM or YYMM), and adds a number of months
PERIOD_ADD(period,months)
For example:
mysql> SELECT PERIOD_ADD(200312,43);
+-----------------------+
| PERIOD_ADD(200312,43) |
+-----------------------+
| 200707 |
+-----------------------+
mysql> SELECT PERIOD_ADD(0312,-32);
+----------------------+
| PERIOD_ADD(0312,-32) |
+----------------------+
| 200104 |
+----------------------+
PERIOD_DIFF(period,period)
For example:
mysql> SELECT PERIOD_DIFF(200104,0312);
+--------------------------+
| PERIOD_DIFF(200104,0312) |
+--------------------------+
| -32 |
+--------------------------+
%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 |
+--------------------------------------+
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 |
+------------------------------+
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.
- ADDDATE(datetime, INTERVAL expression datetimetype)
A synonym for DATE_ADD() - CURDATE()
A synonym for CURRENT_DATE() - CURRENT_DATE()
Returns the current system date as YYYY-MM-DD (or YYYYMMDD if the context requires this, such as when you add a numeric to the result)
mysql> SELECT CURRENT_DATE();
+----------------+
| CURRENT_DATE() |
+----------------+
| 2003-04-14 |
+----------------+
Returns the current system time as hh:mm:ss (or hhmmss if the context requires this, such as when you add a numeric to the result)
A synonym for NOW()
mysql> SELECT CURRENT_TIME();
+----------------+
| CURRENT_TIME() |
+----------------+
| 12:03:10 |
+----------------+
A synonym for CURRENT_TIME()
Adds the expression to the datetime supplied. The expression can be any valid datetimetype.
mysql> SELECT DATE_ADD('2003-07-13', INTERVAL 14 DAY);
+-----------------------------------------+
| DATE_ADD('2003-07-13', INTERVAL 14 DAY) |
+-----------------------------------------+
| 2003-07-27 |
+-----------------------------------------+
Formats the datetime according to the format string
mysql> SELECT DATE_FORMAT('2003-07-14','%b %d,%Y');
+--------------------------------------+
| DATE_FORMAT('2003-07-14','%b %d,%Y') |
+--------------------------------------+
| Jul 14,2003 |
+--------------------------------------+
The same as DATE_ADD, but subtracting instead of adding
mysql> SELECT DATE_SUB('2003-07-14', INTERVAL 14 DAY);
+-----------------------------------------+
| DATE_SUB('2003-07-14', INTERVAL 14 DAY) |
+-----------------------------------------+
| 2003-06-30 |
+-----------------------------------------+
Returns the full name of the day for the specified date.
mysql> SELECT DAYNAME('2003-07-14');
+-----------------------+
| DAYNAME('2003-07-14') |
+-----------------------+
| Monday |
+-----------------------+
Returns a numeric (from 1-31) for the day of the month
mysql> SELECT DAYOFMONTH('2003-07-14'); +--------------------------+ | DAYOFMONTH('2003-07-14') | +--------------------------+ | 14 | +--------------------------+