在应用时,经常要使用这两个函数TIMESTAMPDIFF和TIMESTAMPADD。
一,TIMESTAMPDIFF
语法:
TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2)。
说明:
返回日期或日期时间表达式datetime_expr1 和datetime_expr2the 之间的整数差。其结果的单位由interval 参数给出。该参数必须是以下值的其中一个:
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)
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)
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)