| 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 |
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 |
+----------------------+
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) |
mysql> SELECT DATE_FORMAT('2003-07-14','%b %d,%Y');
+--------------------------------------+
| DATE_FORMAT('2003-07-14','%b %d,%Y') |
+--------------------------------------+
| Jul 14,2003 |
+--------------------------------------+
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.
mysql> SELECT CURRENT_DATE();
+----------------+
| CURRENT_DATE() |
+----------------+
| 2003-04-14 |
+----------------+
mysql> SELECT CURRENT_TIME();
+----------------+
| CURRENT_TIME() |
+----------------+
| 12:03:10 |
+----------------+
mysql> SELECT DATE_ADD('2003-07-13', INTERVAL 14 DAY);
+-----------------------------------------+
| DATE_ADD('2003-07-13', INTERVAL 14 DAY) |
+-----------------------------------------+
| 2003-07-27 |
+-----------------------------------------+
mysql> SELECT DATE_FORMAT('2003-07-14','%b %d,%Y');
+--------------------------------------+
| DATE_FORMAT('2003-07-14','%b %d,%Y') |
+--------------------------------------+
| Jul 14,2003 |
+--------------------------------------+
mysql> SELECT DATE_SUB('2003-07-14', INTERVAL 14 DAY);
+-----------------------------------------+
| DATE_SUB('2003-07-14', INTERVAL 14 DAY) |
+-----------------------------------------+
| 2003-06-30 |
+-----------------------------------------+
mysql> SELECT DAYNAME('2003-07-14');
+-----------------------+
| DAYNAME('2003-07-14') |
+-----------------------+
| Monday |
+-----------------------+
mysql> SELECT DAYOFMONTH('2003-07-14');
+--------------------------+
| DAYOFMONTH('2003-07-14') |
+--------------------------+
| 14 |
+--------------------------+