w3resource logo


MySQL WEEKDAY function

MySQL WEEKDAY() function

rating Average rating 8 out of 10. Total 7 users rated.

<<PreviousNext>>

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 :

The following statement will returns the index of the week for the date 2009-05-19.

SELECT WEEKDAY('2009-05-19');

Output

MySQL WEEKDAY()

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>

View the example in browser

Example : WEEKDAY() function using on table

The following 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).

Sample table : purchase


  SELECT invoice_no,receive_dt,WEEKDAY(receive_dt)

      FROM purchase           

      WHERE WEEKDAY(receive_dt)>0 

      AND WEEKDAY(receive_dt)<6;  

Output

MySQL WEEKDAY() 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-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>

 

View the example in browser

All Date and Time Functions

MySQL Date and Time Functions, slide presentation



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

<<PreviousNext>>