MySQL STR_TO_DATE() function
has average rating
7
out of 10.
Total 15 users rated.
Description
MySQL STR_TO_DATE() returns a datetime value by taking a string and a specific format string as arguments.
If the date or time or datetime value specified as string is illegal, the function returns NULL. The format specifiers have been described in DATE_FORMAT() work with this function also.
Syntax
STR_TO_DATE(str,format);
Arguments
| Name | Description |
|---|---|
| str | A string. |
| format | A date format. |
Example :
Code
SELECT STR_TO_DATE('18,05,2009','%d,%m,%Y');
Explanation
The above statement will return a valid date from the given string 18,05,2009 according to the format %d,%m,%Y.
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-STR_TO_DATE-function - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Getting a valid date from the given string according to the format '%d,%m,%Y' : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Valid date</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT STR_TO_DATE('18,05,2009','%d,%m,%Y')");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row["STR_TO_DATE('18,05,2009','%d,%m,%Y')"] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
Example : STR_TO_DATE() function using (%m/%d/%Y) format
Code
SELECT STR_TO_DATE('05/18/2009', '%m/%d/%Y');
Explanation
The above statement will return a valid date from the given string 05/18/2009 according to the format %m/%d/%Y.
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>example2-STR_TO_DATE-function - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Getting a valid date from the given string according to the format '%m/%d/%Y' : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Valid date</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT STR_TO_DATE('05/18/2009', '%m/%d/%Y')");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row["STR_TO_DATE('05/18/2009', '%m/%d/%Y')"] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
Example : STR_TO_DATE() function using %M %d,%Y
Code
SELECT STR_TO_DATE('May 18, 2009','%M %d,%Y');
Explanation
The above statement will return a valid date from the given string May 18, 2009 according to the format %M %d,%Y.
Output
-example1.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-STR_TO_DATE-function - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Gettting a valid date from the given string according to the format '%M %d,%Y' : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>Valid date</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT STR_TO_DATE('May 18, 2009','%M %d,%Y')");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row["STR_TO_DATE('May 18, 2009','%M %d,%Y')"] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
Example : STR_TO_DATE() function using %h:%i:%s
Code
SELECT STR_TO_DATE('11:59:59','%h:%i:%s');
Explanation
The above statement will return a valid time from the given string 11:59:59 according to the format %h:%i:%s.
Output
-example2.gif)
Code
SELECT STR_TO_DATE('11:59:59','%h %i %s');
Explanation
The above statement will return NULL because the format specifier %h %i %s (HOUR MINUTE SECOND) is not compatible with the contains of the string 11:59:59 (HOUR:MINUTE:SECOND).
Output
-example3.gif)
Example : STR_TO_DATE() function using %W %D %M %Y %H:%i:%s format
Code
SELECT STR_TO_DATE('Monday 15th September 2008 22:23:00', '%W %D %M %Y %H:%i:%s');
Explanation
The above statement will return a valid datetime from the given string Monday 15th September 2008 22:23:00 according to the format %W %D %M %Y %H:%i:%s.
Output
-example4.gif)

