Jump to: navigation, search

Mysql TIMESTAMPDIFF

From w3cyberlearnings

Contents

MySQL TIMESTAMPDIFF Function

This function substracts the second date or datetime expression from the first date or datetime expression.

Syntax TIMESTAMPDIFF

  • Unit: A given unit for interval to be substract
    • MICROSECOND
    • SECOND
    • MINUTE
    • HOUR
    • DAY
    • WEEK
    • MONTH
    • QUARTER
    • YEAR
  • expre1: first date or datetime expression
  • expre2: second date or datetime expression
TIMESTAMPDIFF(Unit, expre1, expre2);

Example 1

mysql> SELECT TIMESTAMPDIFF(YEAR,'2012-4-04','2015-4-02');
+---------------------------------------------+
| TIMESTAMPDIFF(YEAR,'2012-4-04','2015-4-02') |
+---------------------------------------------+
|                                           2 |
+---------------------------------------------+
1 row in set (0.00 sec)

Example 2

mysql> SELECT TIMESTAMPDIFF(DAY,'2012-4-04','2015-4-02');
+--------------------------------------------+
| TIMESTAMPDIFF(DAY,'2012-4-04','2015-4-02') |
+--------------------------------------------+
|                                       1093 |
+--------------------------------------------+
1 row in set (0.00 sec)

Example 3

mysql> SELECT TIMESTAMPDIFF(WEEK,'2012-4-04','2015-4-02');
+---------------------------------------------+
| TIMESTAMPDIFF(WEEK,'2012-4-04','2015-4-02') |
+---------------------------------------------+
|                                         156 |
+---------------------------------------------+
1 row in set (0.00 sec)

Related Links



Navigation
Web
SQL
MISC
References