w3resource logo


MySQL SUBDATE function

MySQL SUBDATE() function

rating Average rating 6 out of 10. Total 5 users rated.

<<PreviousNext>>

Description

MySQL SUBDATE() subtracts a time value (as interval) from a given date.

DATE_SUB() and SUBDATE are synonyms of SUBDATE().

Syntax

SUBDATE(date, INTERVAL expr unit)

Arguments

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

Example :

The following statement will return a date after subtracting 10 days (notice that INTERVAL keyword is used) from the specified date 2008-05-15.

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

Output

MySQL SUBDATE()

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-SUBDATE-function - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Calculate a date after subtracting 10 days from 2008-05-15 : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Calculated output</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT SUBDATE('2008-05-15', INTERVAL 10 DAY)");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row["SUBDATE('2008-05-15', INTERVAL 10 DAY)"] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
  </html>
  

View the example in browser

Example : SUBDATE() function without INTERVAL keyword

The following statement will return a date after subtracting 10 days (notice that INTERVAL keyword is not used) from the specified date 2008-05-15.

SELECT SUBDATE('2008-05-15', 10);

Output

MySQL SUBDATE() 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-SUBDATE-function - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Calculate a date after subtracting 10 days from 2008-05-15 : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Calculated output</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT SUBDATE('2008-05-15', 10)");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row["SUBDATE('2008-05-15', 10)"] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

View the example in browser



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

<<PreviousNext>>