w3resource

MySQL DATE_SUB() function

DATE_SUB() function

MySQL DATE_SUB() function subtract a time value (as interval) from a date.

Syntax:

DATE_SUB(date, INTERVAL expr unit)

Arguments

Name Description
date A date value.
INTERVAL Keyword.
expr A date or datetime expression of a number.
unit An unit, described in the table.

Video Presentation

Pictorial Presentation

Pictorial Presentation of MySQL DATE_SUB() function

Example: MySQL DATE_SUB() function

The following statement will return a date after subtracting 10 days from the specified date 2008-05-15.

Code:

SELECT DATE_SUB('2008-05-15', INTERVAL 10 DAY);

Sample Output:

mysql> SELECT DATE_SUB('2008-05-15', INTERVAL 10 DAY);
+-----------------------------------------+
| DATE_SUB('2008-05-15', INTERVAL 10 DAY) |
+-----------------------------------------+
| 2008-05-05                              | 
+-----------------------------------------+
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-DATE_SUB-function - php mysql examples | w3resource</title>
<meta name="description" content="example-DATE_SUB-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>Showing the date after subtracting ten days from 2008-05-15:</h2>
<table class='table table-bordered'>
<tr>
<th>Calculated date</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 DATE_SUB("2008-05-15", INTERVAL 10 DAY)') as $row) {
echo "<tr>";
echo "<td>" . $row['DATE_SUB("2008-05-15", INTERVAL 10 DAY)'] . "</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-date_sub-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 DATE_SUB('2008-05-15', INTERVAL 10 DAY)";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Calculated date</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("DATE_SUB('2008-05-15', INTERVAL 10 DAY)")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example : DATE_SUB() function with 'DAY_SECOND'

The following statement will return a datetime after subtracting 1 1:10:10 DAY_SECOND from 2008-05-15 4:50:20.

Code:

SELECT DATE_SUB('2008-05-15 4:50:20',INTERVAL '1 1:10:10' DAY_SECOND);

Sample Output:

mysql> SELECT DATE_SUB('2008-05-15 4:50:20',INTERVAL '1 1:10:10' DAY_SECOND);
+----------------------------------------------------------------+
| DATE_SUB('2008-05-15 4:50:20',INTERVAL '1 1:10:10' DAY_SECOND) |
+----------------------------------------------------------------+
| 2008-05-14 03:40:10                                            | 
+----------------------------------------------------------------+
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-DATE_SUB-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-DATE_SUB-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 datetime using MySQL:</h2>
<table class='table table-bordered'>
<tr>
<th>Calculated date</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 DATE_SUB("2008-05-15  4:50:20",INTERVAL "1 1:10:10" DAY_SECOND);') as $row) {
echo "<tr>";
echo "<td>" . $row['DATE_SUB("2008-05-15  4:50:20",INTERVAL "1 1:10:10" DAY_SECOND)'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Examplep: DATE_SUB() function with 'INTERVAL'

The following statement will return a datetime after subtracting 1 SECOND from 2008-05-15.

Code:

SELECT '2008-05-15' - INTERVAL 1 SECOND;

Sample Output:

mysql> SELECT '2008-05-15' - INTERVAL 1 SECOND;
+----------------------------------+
| '2008-05-15' - INTERVAL 1 SECOND |
+----------------------------------+
| 2008-05-14 23:59:59              | 
+----------------------------------+
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>example2-DATE_SUB-function - php mysql examples | w3resource</title>
<meta name="description" content="example2-DATE_SUB-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 datetime using MySQL:</h2>
<table class='table table-bordered'>
<tr> 
<th>Calculated date</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 "2008-05-15" - INTERVAL 1 SECOND') as $row) {
echo "<tr>";
echo "<td>" . $row['"2008-05-15" - INTERVAL 1 SECOND'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Example : DATE_SUB() function with plus(+) operator

The following statement will return a datetime after adding 1 HOUR with 2008-05-15. This example shows that the INTERVAL to be subtracted (or added) can also be specified before the original date / datetime.

Code:

SELECT INTERVAL 1 HOUR + '2008-05-15';

Sample Output:

mysql> SELECT INTERVAL 1 HOUR + '2008-05-15';
+--------------------------------+
| INTERVAL 1 HOUR + '2008-05-15' |
+--------------------------------+
| 2008-05-15 01:00:00            | 
+--------------------------------+
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>example3-DATE_SUB-function - php mysql examples | w3resource</title>
<meta name="description" content="example3-DATE_SUB-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 datetime using MySQL:</h2>
<table class='table table-bordered'>
<tr>
<th>Calculated date</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 INTERVAL 1 HOUR + "2008-05-15"') as $row) {
echo "<tr>";
echo "<td>" . $row['INTERVAL 1 HOUR + "2008-05-15"'] . "</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.

Previous: DATE_FORMAT()
Next: DATE()