w3resource logo


MySQL timestampdiff function

MySQL TIMESTAMPDIFF() function

rating Average rating 7 out of 10. Total 16 users rated.

<<PreviousNext>>

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.

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 TIMESTAMPDIFF()

PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>example-timestampdiff-function - php mysql examples | w3resource</title>

</head>

<body>

<?php

echo "<h2>Difference of dates in terms of month between 2009-05-18 and 2009-07-29 : </h2>";

echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";

echo "<tr style='font-weight: bold;'>";

echo "<td width='100' align='center'>Difference in months</td>";

echo "</tr>";

include("../dbopen.php");

$result = mysql_query("SELECT TIMESTAMPDIFF(MONTH,'2009-05-18','2009-07-29')");

while($row=mysql_fetch_array($result))

{

echo "<tr>";

echo "<td align='center' width='200'>" . $row["TIMESTAMPDIFF(MONTH,'2009-05-18','2009-07-29')"] . "</td>";

echo "</tr>";

}

echo "</table>";

?>

</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 TIMESTAMPDIFF() EXAMPLE

PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>example1-timestampdiff-function - php mysql examples | w3resource</title>

</head>

<body>

<?php

echo "<h2>Difference in minutes between 2009-05-18 11:45:42 and 2009-05-20 15:16:39 : </h2>";

echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";

echo "<tr style='font-weight: bold;'>";

echo "<td width='100' align='center'>Difference in months</td>";

echo "</tr>";

include("../dbopen.php");

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

while($row=mysql_fetch_array($result))

{

echo "<tr>";

echo "<td align='center' width='200'>" . $row["TIMESTAMPDIFF(MINUTE,'2009-05-18 11:45:42','2009-05-20 15:16:39')"] . "</td>";

echo "</tr>";

}

echo "</table>";

?>

</body>

</html>

View the example in browser

All Date and Time Functions

MySQL Date and Time Functions, slide presentation



We have compiled a number of MySQL Questions and Answers. Check and assess you MySQL skill by visiting those questions and answers.

<<PreviousNext>>