w3resource logo


MySQL timestampdiff function

MySQL TIMESTAMPDIFF() function

<<PreviousNext>>

Seondary Nav

Description

MySQL the TIMESTAMPDIFF() returns a value after subtracting a datetime expression from another.

It is not necessary that both the expression are of 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.

Pictorial Presentation

Pictorial Presentation of MySQL TIMESTAMPDIFF() function

Example :

The following statement will return a value in months by subtracting 2009-05-18 from 2009-07-29.

SELECT TIMESTAMPDIFF(MONTH,'2009-05-18','2009-07-29');  

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">
<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>

View the example in browser

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.

SELECT TIMESTAMPDIFF(MINUTE,'2009-05-18 11:45:42','2009-05-20 15:16:39');

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">
<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>

View the example in browser

All Date and Time Functions :

Click here to see the MySQL Date and time functions.



<<PreviousNext>>

Looking for some other tutorial?