In MySQL, The TIMESTAMPDIFF and TIMESTAMPADD functions are often used in applications. 1. TIMESTAMPDIFF Syntax: TIMESTAMPDIFF (interval, datetime_expr1, datetime_expr2 ). Note: www.2cto.com returns the integer difference between the datetime_expr1 and datetime_expr2the expressions of date and time. The unit of the result is given by the interval parameter. This parameter must be one of the following values: FRAC_SECOND. The interval is millisecond SECOND. Second MINUTE. Minutes HOUR. Hour DAY. Day WEEK. Week MONTH. Month QUARTER. Quarter YEAR. Year
Use the following statement: [SQL] mysql> select TIMESTAMPDIFF (day, '2017-08-24 ', '2017-08-30'); + hour + | TIMESTAMPDIFF (day, '2017-08-24 ', '2017-08-30 ') | + hour + | 6 | + -------------------------------------------- + 1 row in set (2012 sec) www.2cto.com [SQL] mysql> select TIMESTAMPDIFF (MINUTE, '2017-08-24 09:00:00 ', '2017-08-30 12:00:00'); + --- -------------------------------------------------------------- + | TIMESTAMPDIFF (MINUTE, '2017-08-24 09:00:00 ', '2017-08-30 12:00:00 ') | + keys + | 8820 | + keys + 1 row in set (0.01 sec) II. TIMESTAMPADD Syntax: TIMESTAMPADD (interval, int_expr, datetime_expr) www.2cto.com Description: int_ex Add pr to the date or datetime expression datetime_expr. The value of interval is the same as the value listed above.
[SQL] mysql> select TIMESTAMPADD (MINUTE, 8820, '2017-08-24 09:00:00 '); + ----------------------------------------------- + | TIMESTAMPADD (MINUTE, 2012, '2017-08-24 09:00:00 ') | + ------------------------------------------------- + | 2012 12:00:00 | + rows + 1 row in set (0.00 sec)