w3resource

MySQL PERIOD_DIFF() function

PERIOD_DIFF() function

MySQL PERIOD_DIFF() returns the difference between two periods. Periods should be in the same format i.e. YYYYMM or YYMM. It is to be noted that periods are not date values.

Syntax:

PERIOD_DIFF(P1,P2);

Arguments :

Name Description
P1 Indicates a period.
P2 Indicates a period.

Syntax Diagram:

MySQL PERIOD_DIFF() Function - Syntax Diagram

MySQL Version: 5.6


Video Presentation

Pictorial Presentation

Pictorial Presentation of MySQL PERIOD_DIFF() function

Example: MySQL PERIOD_DIFF() function

The following statement will return a value in YYYYMM or YYMM format after calculating the difference between two periods 200905 and 200811.

Code:

SELECT PERIOD_DIFF(200905,200811);

Sample Output:

mysql> SELECT PERIOD_DIFF(200905,200811);
+----------------------------+
| PERIOD_DIFF(200905,200811) |
+----------------------------+
|                          6 | 
+----------------------------+
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-PERIOD_DIFF-function - php mysql examples | w3resource</title>
<meta name="description" content="example-PERIOD_DIFF-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 between May 2009 and November 2008 :</h2>
<table class='table table-bordered'>
<tr>
<th>Calculated 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 PERIOD_DIFF(200905,200811)') as $row) {
echo "<tr>";
echo "<td>" . $row['PERIOD_DIFF(200905,200811)'] . "</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-period_diff-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 PERIOD_DIFF(200905,200811)";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Calculated difference</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("PERIOD_DIFF(200905,200811)")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

All Date and Time Functions:

Click here to see the MySQL Date and time functions.

Previous: PERIOD_ADD()
Next: QUARTER()