w3resource

MySQL TIMESTAMPADD() function

TIMESTAMPADD() function

MySQL TIMESTAMPADD() adds time value with a date or datetime value.

The unit for the interval as mentioned should be one of the following : FRAC_SECOND(microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER or YEAR.

Syntax:

TIMESTAMPADD(unit,interval,datetime_expr);

Arguments:

Name Description
datetime_expr A date or datetime expression or a number.
interval An integer.
unit An unit, as described in the description.

Syntax Diagram:

MySQL TIMESTAMPADD() Function - Syntax Diagram

MySQL Version: 5.6


Video Presentation

Pictorial Presentation

Pictorial Presentation of MySQL TIMESTAMPADD() function

Example:

The following statement will return a date value after adding 2 months with the specified date 2009-05-18.

Code:

SELECT TIMESTAMPADD(MONTH,2,'2009-05-18');

Sample Output:

mysql> SELECT TIMESTAMPADD(MONTH,2,'2009-05-18');
+------------------------------------+
| TIMESTAMPADD(MONTH,2,'2009-05-18') |
+------------------------------------+
| 2009-07-18                         | 
+------------------------------------+
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-TIMESTAMPADD-function - php mysql examples | w3resource</title>
<meta name="description" content="example-TIMESTAMPADD-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>Date value after adding two months to 2009-05-18:</h2>
<table class='table table-bordered'>
<tr>
<th>Date value</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 TIMESTAMPADD(MONTH,2,"2009-05-18")') as $row) {
echo "<tr>";
echo "<td>" . $row['TIMESTAMPADD(MONTH,2,"2009-05-18")'] . "</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-timestampadd-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 TIMESTAMPADD(MONTH,2,'2009-05-18')";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Date value</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("TIMESTAMPADD(MONTH,2,'2009-05-18')")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example : TIMESTAMPADD() function in WEEK

The following statement will return a date value after adding 1 week with the specified date 2009-05-18.

Code:

SELECT TIMESTAMPADD(WEEK,1,'2009-05-18');

Sample Output:

mysql> SELECT TIMESTAMPADD(WEEK,1,'2009-05-18');
+-----------------------------------+
| TIMESTAMPADD(WEEK,1,'2009-05-18') |
+-----------------------------------+
| 2009-05-25                        | 
+-----------------------------------+
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-TIMESTAMPADD-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-TIMESTAMPADD-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>Date value after adding one week to 2009-05-18:</h2>
<table class='table table-bordered'>
<tr>
<th>Date value</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 TIMESTAMPADD(WEEK,1,"2009-05-18")') as $row) {
echo "<tr>";
echo "<td>" . $row['TIMESTAMPADD(WEEK,1,"2009-05-18")'] . "</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: TIMEDIFF()
Next: TIMESTAMPDIFF()