MySQL WEEKDAY() function
has average rating
6
out of 10.
Total 1 users rated.
Description
MySQL WEEKDAY() returns the index of the day in a week for a given date (0 for Monday, 1 for Tuesday and ......6 for Sunday).
Syntax
WEEKDAY(date)
Where date is a date.
Example :
Code
SELECT WEEKDAY('2009-05-19');
Explanation
The above statement will returns the index of the week for the date 2009-05-19.
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-WEEKDAY-function - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Index number of the specified date 2009-05-18 in the week. : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='400' align='center'>Index number of the specified date 2009-05-18 in the week</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT WEEKDAY('2009-05-19')");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row["WEEKDAY('2009-05-19')"] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
Example : WEEKDAY() function using on table
Sample table : purchase
Code
SELECT invoice_no,receive_dt,WEEKDAY(receive_dt)
FROM purchase
WHERE WEEKDAY(receive_dt)>0
AND WEEKDAY(receive_dt)<6;
Explanation
The above statement will return the invoice_no, receive_dt and index of the week for receive_dt, after making sure that index of the week for receive_dt must be more than 0 and less than 6 (i.e. it should be from Monday through Saturday).
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-WEEKDAY-function - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Invoice no, Received date and WEEKDAY of received date where index of WEEKDAY of received date is between 1 to 5 (i.e. it should be Monday to Friday) : </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'>Received date</td><td width='100' align='center'>WEEKDAY of received date</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT invoice_no,receive_dt,WEEKDAY(receive_dt)
FROM purchase
WHERE WEEKDAY(receive_dt)>0
AND WEEKDAY(receive_dt)<6");
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['receive_dt'] . "</td>";
echo "<td align='center' width='200'>" . $row['WEEKDAY(receive_dt)'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

