w3resource logo


MySQL CURDATE function

MySQL CURDATE() function

rating has average rating 8 out of 10. Total 2 users rated.

<<PreviousNext>>

Description

In MySQL the CURDATE() returns the current date in 'YYYY-MM-DD' format or 'YYYYMMDD' format depending on whether numeric or string is used in the function. CURRENT_DATE and CURRENT_DATE() are the synonym of CURDATE().

Note: All of the example codes of this page will produce outputs depending upon the current date.

Syntax

CURDATE ()

Example :

Code

SELECT CURDATE();

Explanation

The above statement will return the current date in ‘YYYY-MM-DD’ format.

Output

MySQL CURDATE()

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-CURDATE-function - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Select current date using MySQL : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Required Date</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT CURDATE()");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['CURDATE()'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

View the example in browser

Example : CURDATE() function in numeric format

Code

SELECT CURDATE()+1;

Explanation

The above statement will return the current date in 'YYYYMMDD' format. Because the context of the function is numeric.

Output

MySQL CURDATE() 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-CURDATE-function - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Showing one day after current date in YYYYMMDD format using MySQL : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Required Date</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT CURDATE()+1");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['CURDATE()+1'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

View the example in browser

photo credit: jemasmith. Photo is used under creative Common License.

<<PreviousNext>>