MySQL QUARTER() function
Description
MySQL QUARTER() returns the quarter of the year for a date. The return value is in the range of 1 to 4.
Syntax
QUARTER(date);
Where date is a date.
Example :
Code
SELECT QUARTER('2009-05-18');
Explanation
The above statement will return a value between 1 to 4 as a QUARTER of a year for a given date 2009-05-18.
Output
.gif)
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-QUARTER-function - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Calculating a value between 1 to 4 as a QUARTER of a year for a given date 2009-05-18 : </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 QUARTER('2009-05-18')");
while($row=mysql_fetch_array($result))
{
echo "<tr>";>
echo "<td align='center' width='200'>" . $row["QUARTER('2009-05-18')"] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
Example : QUARTER() function using table
Sample table : purchase
Code
SELECT invoice_no,ord_date,QUARTER(ord_date) FROM purchase WHERE QUARTER(ord_date)=2 ;
Explanation
The above statement will return a value between 1 to 4 as a QUARTER of a year for ord_date from the table purchase making sure that the ord_date belongs to the 2nd QUARTER.
Output
-example.gif)
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-QUARTER-function - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>List of invoice number, order date and in which QUARTER orders are placed. Order which are placed in the second QUARTER, are listed only : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Invoice no.</td><td width='100' align='center'>Order date</td>
<td width='100' align='center'>In which QUARTER the order is placed</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT invoice_no,ord_date,QUARTER(ord_date)
FROM purchase
WHERE QUARTER(ord_date)=2");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['invoice_no'] . "</td>";
echo "<td align='center' width='200'>" . $row['ord_date'] . "</td>";
echo "<td align='center' width='200'>" . $row['QUARTER(ord_date)'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

