Usage of TIMESTAMPDIFF and TIMESTAMPADD functions in MySQL

        In application, these two functions TIMESTAMPDIFF and TIMESTAMPADD are often used.

1.TIMESTAMPDIFF

grammar:

        TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2)。

illustrate:

        Returns the integer difference between the date or datetime expressions datetime_expr1 and datetime_expr2the. The unit of the result is given by the interval parameter. This parameter must be one of the following values:

        FRAC_SECOND: Indicates that the interval is in milliseconds

        SECOND: seconds

        MINUTE: minutes

        HOUR: hours

        DAY: day

        WEEK: week

        MONTH: month

        QUARTER: Quarterly

        YEAR: year

Use as follows:

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)  

 

二.TIMESTAMPADD

grammar:

        TIMESTAMPADD(interval,int_expr,datetime_expr)

illustrate:

        Add the integer expression int_expr to the date or datetime expression datetime_expr. The interval in the formula is the same as the value listed above.

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)  

 

Article source: http://blog.csdn.net/zmxiangde_88/article/details/8011661

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326801473&siteId=291194637
Recommended