w3resource

MySQL DATEDIFF() function

DATEDIFF() function

MySQL DATEDIFF() returns the number of days between two dates or datetimes. This function only calculates the date portion from each expression.

Syntax:

DATEDIFF(expr1,expr2);

Arguments:

Name Description
expr1 A datetime expression.
expr2 A datetime expression.

Syntax Diagram:

MySQL DATEDIFF() Function - Syntax Diagram

MySQL Version: 5.6


Video Presentation:>

Pictorial Presentation:

Pictorial Presentation of MySQL DATEDIFF() function

Example: MySQL DATEDIFF() function

The following statement will return the days between two datetime expressions 2008-05-17 11:31:31 and 2008-04-28.

Code:

SELECT DATEDIFF('2008-05-17 11:31:31','2008-04-28');

Sample Output:

mysql> SELECT DATEDIFF('2008-05-17 11:31:31','2008-04-28');
+----------------------------------------------+
| DATEDIFF('2008-05-17 11:31:31','2008-04-28') |
+----------------------------------------------+
|                                           19 | 
+----------------------------------------------+
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-DATEDIFF-function - php mysql examples | w3resource</title>
<meta name="description" content="example-DATEDIFF-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>Calculating difference of dates between 2008-05-17 11:31:31 and 2008-04-28 using MySQL:</h2>
<table class='table table-bordered'>
<tr>
<th>Date difference</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 DATEDIFF("2008-05-17 11:31:31","2008-04-28") as date_difference') as $row) {
echo "<tr>";
echo "<td>" . $row['date_difference'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

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-datediff-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 DATEDIFF('2008-05-17 11:31:31','2008-04-28') as date_difference";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Date difference</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("date_difference")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example : DATEDIFF() function using table

The following statement will return those rows from the table purchase in which the difference of days between ‘invoice_dt’ and ‘ord_date’ are less than 10.

Sample table: purchase


Code:

SELECT invoice_dt,ord_date,DATEDIFF(invoice_dt,ord_date)
FROM purchase           
WHERE DATEDIFF(invoice_dt,ord_date)<10;  

Sample Output:

mysql> SELECT invoice_dt,ord_date,DATEDIFF(invoice_dt,ord_date)
    -> FROM purchase           
    -> WHERE DATEDIFF(invoice_dt,ord_date)< 10; 
+------------+------------+-------------------------------+
| invoice_dt | ord_date   | DATEDIFF(invoice_dt,ord_date) |
+------------+------------+-------------------------------+
| 2008-07-15 | 2008-07-06 |                             9 | 
| 2008-09-20 | 2008-09-15 |                             5 | 
| 2007-08-30 | 2007-08-22 |                             8 | 
| 2007-09-24 | 2007-09-20 |                             4 | 
+------------+------------+-------------------------------+
4 rows 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-DATEDIFF-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-DATEDIFF-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>Calculating difference of dates between date of invoice and date of order. List includes the values if the date difference is less than ten:</h2>
<table class='table table-bordered'>
<tr>
<th>Date of invoice</th><th>Order date</th><th>Difference of dates between date of invoice and date of order</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 invoice_dt,ord_date,DATEDIFF(invoice_dt,ord_date)
FROM purchase
WHERE DATEDIFF(invoice_dt,ord_date)<10') as $row) {
echo "<tr>";
echo "<td>" . $row['invoice_dt'] . "</td>";
echo "<td>" . $row['ord_date'] . "</td>"; 
echo "<td>" . $row['DATEDIFF(invoice_dt,ord_date)'] . "</td>";
echo "</tr>";  
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Online Practice Editor:


All Date and Time Functions :

Click here to see the MySQL Date and time functions.

Previous: DATE()
Next: DAY()



Follow us on Facebook and Twitter for latest update.