These two functions, Timestampdiff and timestampadd, are often used when applying.
One, Timestampdiff
Grammar:
Timestampdiff (INTERVAL,DATETIME_EXPR1,DATETIME_EXPR2).
Description
Returns an integer difference between a date or datetime expression Datetime_expr1 and datetime_expr2the. The unit of the result is given by the interval parameter. The parameter must be one of the following values:
- Frac_second. Indicates that the interval is milliseconds
- SECOND. Seconds
- MINUTE. Minutes
- HOUR. Hours
- Day. Days
- WEEK. Week
- MONTH. Month
- QUARTER. Quarter
- Year. Years
Use the following:
[SQL]View PlainCopy
- mysql> Select Timestampdiff (Day,' 2012-08-24 ',' 2012-08-30 ');
- +----------------------------------------------+
- | Timestampdiff (Day,' 2012-08-24 ',' 2012-08-30 ') |
- +----------------------------------------------+
- | 6 |
- +----------------------------------------------+
- 1 row in Set (0.00 sec)
[SQL]View PlainCopy
- mysql> Select Timestampdiff (MINUTE,' 2012-08-24 09:00:00 ',' 2012-08-30 12:00:00 ');
- +-------------------------------------------------------------------+
- | Timestampdiff (MINUTE,' 2012-08-24 09:00:00 ',' 2012-08-30 12:00:00 ') |
- +-------------------------------------------------------------------+
- | 8820 |
- +-------------------------------------------------------------------+
- 1 row in Set (0.01 sec)
Two, Timestampadd syntax: Timestampadd (interval,int_expr,datetime_expr) Description: Adds an integer expression int_expr to a date or datetime expression datetime_expr. The interval in the formula are the same as the values listed above.
[SQL]View PlainCopy
- mysql> Select Timestampadd (minute,8820,' 2012-08-24 09:00:00 ');
- +-------------------------------------------------+
- | Timestampadd (minute,8820,' 2012-08-24 09:00:00 ') |
- +-------------------------------------------------+
- | 2012-08-30 12:00:00 |
- +-------------------------------------------------+
- 1 row in Set (0.00 sec)
MySQL Timestampdiff and Timestampadd functions