Last time we looked at a few of the more commonly used date and time functions. This time we're going to look at some of the less well-known functions. First, we will need to understand the various date and time types MySQL accepts for passing to many of these functions.
As you can see, there are quite a few. Notice that the ones spanning a number of time elements (such as DAY_SECOND) include all the elements between DAY and SECOND (in this case HOUR and MINUTE). In the last article, we looked at a specific date calculation to determine age. However, there's a lot more you can do in MySQL before you need to call upon a programming language for assistance.
Adding and subtracting dates and times with DATE_ADD() and DATE_SUB()
The DATE_ADD() function - ADDDATE() is a synonmym - is used to add a particular date or time interval to a give date or time.
DATE_ADD(datetime, INTERVAL expression datetimetype)
For example, to find a date 14 days after the 13th July, 2003, you can use:
Note that when using a datetime type that requires more than a simple numeric, you need to use quotes to contain the entire expression. You can also mix date and time types, and MySQL will do its best to make do. For example:
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
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:
Abbreviation of the day (from Sun-Sat)
Abbreviation of the month (from Jan-Dec)
Numeric month (from 1-12)
Numeric day of the month with suffix (1st, 2nd, and so on)
Numeric day of the month with two digits(from 00-31)
Numeric day of the month with one or two digits(from 0-31)
Hour (from 00-23)
Hour (from 01-12)
Minutes (from 00-59)
Hour (from 01-12)
Day of the year (from 001-366)
Hour with one or two digits (from 0-23)
Hour with one digit (from 1-12)
Month name (from January-December)
Numeric month (from 01-12)
A.M. or P.M.
12-hour time (hh:mm:ss A.M.or P.M.)
Seconds (from 00-59)
Seconds (from 00-59)
24 hour time (hh:mm:ss)
Week (from 00-53, Sunday being the first day of the week)
Week (from 00-53, Monday being the first day of the week)
Week (from 01-53, Sunday being the first day of the week)
Week (from 01-53, Monday being the first day of the week)
Name of the day in the week (from Sunday-Saturday)
Day of the week (from 0 - Sunday, to 6 - Saturday)
Four-digit numeric year for the week (Sunday being the first day of the week)
Four-digit numeric year for the week (Monday being the first day of the week)
Four-digit numeric year
Two-digit numeric year
Percentage sign (escaped)
Let's look at converting a standard date into a format used in the US.
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.
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.
Date functions reference
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)
EXTRACT(datetimetype FROM datetime) Returns the portion of the datetime corresponding to the supplied datetimetype
mysql> SELECT EXTRACT(HOUR_MINUTE FROM '2003-07-14 12:32:14');
| EXTRACT(HOUR_MINUTE FROM '2003-07-14 12:32:14') |
| 1232 |
FROM_DAYS(number_of_days) Converts the supplied numeric into a date based upon the number of days since 1 Jan of the year 0 (without taking days lost in the change to the Gregorian calendar into account)
WEEK(datetime [,week_start]) Returns a numeric (from 0-53) for the week of the supplied datetime. Weeks start on Sunday unless the optional week_start argument is set to 1, in which case weeks are assumed to start on Monday