w3resource logo


MySQL STR_TO_DATE function

MySQL STR_TO_DATE() function

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

<<PreviousNext>>

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 :

The following statement will return a valid date from the given string 18,05,2009 according to the format %d,%m,%Y.

SELECT STR_TO_DATE('18,05,2009','%d,%m,%Y');

Output

MySQL STR_TO_DATE()

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>

View the example in browser

Example : STR_TO_DATE() function using (%m/%d/%Y) format

The following statement will return a valid date from the given string 05/18/2009 according to the format %m/%d/%Y.

SELECT STR_TO_DATE('05/18/2009', '%m/%d/%Y');

Output

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

View the example in browser

Example : STR_TO_DATE() function using %M %d,%Y

The following statement will return a valid date from the given string May 18, 2009 according to the format %M %d,%Y.

SELECT STR_TO_DATE('May 18, 2009','%M %d,%Y');

Output

MySQL STR_TO_DATE() EXAMPLE1

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>

View the example in browser

Example : STR_TO_DATE() function using %h:%i:%s

The following statement will return a valid time from the given string 11:59:59 according to the format %h:%i:%s.

SELECT STR_TO_DATE('11:59:59','%h:%i:%s');

Output

MySQL STR_TO_DATE() EXAMPLE2

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

SELECT STR_TO_DATE('11:59:59','%h %i %s');

Output

MySQL STR_TO_DATE() EXAMPLE3

Example : STR_TO_DATE() function using %W %D %M %Y %H:%i:%s format

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

SELECT STR_TO_DATE('Monday 15th September 2008 22:23:00',            '%W %D %M %Y %H:%i:%s');

Output

MySQL STR_TO_DATE() EXAMPLE4



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

<<PreviousNext>>