MySQL TIMESTAMPDIFF() function
TIMESTAMPDIFF() function
MySQL the TIMESTAMPDIFF() returns a value after subtracting a datetime expression from another.
It is not necessary that both the expression are of the same type. One may be a date and another is datetime. A date value is treated as a datetime with a default time part '00:00:00'. The unit for the result is given by another argument.
The unit should be one of the following : FRAC_SECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.
Syntax:
TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2);
Arguments:
Name | Description |
---|---|
datetime_expr1 | A datetime expression. |
datetime_expr1 | A datetime expression. |
unit | An unit, as described in the description. |
Syntax Diagram:

MySQL Version: 5.6
Video Presentation:
Pictorial Presentation:

Example:
The following statement will return a value in months by subtracting 2009-05-18 from 2009-07-29.
Code:
SELECT TIMESTAMPDIFF(MONTH,'2009-05-18','2009-07-29');
Sample Output:
mysql> SELECT TIMESTAMPDIFF(MONTH,'2009-05-18','2009-07-29'); +------------------------------------------------+ | TIMESTAMPDIFF(MONTH,'2009-05-18','2009-07-29') | +------------------------------------------------+ | 2 | +------------------------------------------------+ 1 row in set (0.00 sec)
PHP script:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>example-timestampdiff-function - php mysql examples | w3resource</title>
<meta name="description" content="example-timestampdiff-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Difference of dates in terms of month between 2009-05-18 and 2009-07-29 : </h2>
<table class='table table-bordered'>
<tr>
<th>Difference in months</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT TIMESTAMPDIFF(MONTH,"2009-05-18","2009-07-29")') as $row) {
echo "<tr>";
echo "<td>" . $row['TIMESTAMPDIFF(MONTH,"2009-05-18","2009-07-29")'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>
JSP script:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-timestampdiff-function</title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String Host = "jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data ="SELECT TIMESTAMPDIFF(MONTH,'2009-05-18','2009-07-29')";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Difference in months</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("TIMESTAMPDIFF(MONTH,'2009-05-18','2009-07-29')")%></TD>
</TR>
<% } %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>
Example: TIMESTAMPDIFF() function in MINUTE
The following statement will return a value in minutes after subtracting 2009-05-18 11:45:42 from 2009-05-20 15:16:39.
Code:
SELECT TIMESTAMPDIFF(MINUTE,'2009-05-18 11:45:42','2009-05-20 15:16:39');
Sample Output:
mysql> SELECT TIMESTAMPDIFF(MINUTE,'2009-05-18 11:45:42','2009-05-20 15:16:39'); +-------------------------------------------------------------------+ | TIMESTAMPDIFF(MINUTE,'2009-05-18 11:45:42','2009-05-20 15:16:39') | +-------------------------------------------------------------------+ | 3090 | +-------------------------------------------------------------------+ 1 row in set (0.00 sec)
PHP script:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>example1-timestampdiff-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-timestampdiff-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Difference in minutes between 2009-05-18 11:45:42 and 2009-05-20 15:16:39:</h2>
<table class='table table-bordered'>
<tr>
<th>Difference in months</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT TIMESTAMPDIFF(MINUTE,"2009-05-18 11:45:42","2009-05-20 15:16:39")') as $row) {
echo "<tr>";
echo "<td>" . $row['TIMESTAMPDIFF(MINUTE,"2009-05-18 11:45:42","2009-05-20 15:16:39")'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>
All Date and Time Functions:
Click here to see the MySQL Date and time functions.
Previous: TIMESTAMPADD()
Next: TO_DAYS()
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook