w3resource logo


MySQL STR_TO_DATE function

MySQL STR_TO_DATE() function

<<PreviousNext>>

Seondary Nav

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.

Pictorial Presentation

Pictorial Presentation of MySQL STR_TO_DATE() function

Example: MySQL STR_TO_DATE() function

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> SELECT STR_TO_DATE('18,05,2009','%d,%m,%Y');
+--------------------------------------+
| STR_TO_DATE('18,05,2009','%d,%m,%Y') |
+--------------------------------------+
| 2009-05-18                           | 
+--------------------------------------+
1 row in set (0.00 sec)

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> SELECT STR_TO_DATE('05/18/2009', '%m/%d/%Y');
+---------------------------------------+
| STR_TO_DATE('05/18/2009', '%m/%d/%Y') |
+---------------------------------------+
| 2009-05-18                            | 
+---------------------------------------+
1 row in set (0.00 sec)

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> SELECT STR_TO_DATE('May 18, 2009','%M %d,%Y');
+----------------------------------------+
| STR_TO_DATE('May 18, 2009','%M %d,%Y') |
+----------------------------------------+
| 2009-05-18                             | 
+----------------------------------------+
1 row in set (0.00 sec)

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> SELECT STR_TO_DATE('11:59:59','%h:%i:%s');
+------------------------------------+
| STR_TO_DATE('11:59:59','%h:%i:%s') |
+------------------------------------+
| 11:59:59                           | 
+------------------------------------+
1 row in set (0.00 sec)

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> SELECT STR_TO_DATE('11:59:59','%h %i %s');
+------------------------------------+
| STR_TO_DATE('11:59:59','%h %i %s') |
+------------------------------------+
| NULL                               | 
+------------------------------------+
1 row in set, 1 warning (0.02 sec)

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> SELECT STR_TO_DATE('Monday 15th September 2008 22:23:00', '%W %D %M %Y %H:%i:%s');
+----------------------------------------------------------------------------+
| STR_TO_DATE('Monday 15th September 2008 22:23:00', '%W %D %M %Y %H:%i:%s') |
+----------------------------------------------------------------------------+
| 2008-09-15 22:23:00                                                        | 
+----------------------------------------------------------------------------+
1 row in set (0.00 sec)
 

All Date and Time Functions :

Click here to see the MySQL Date and time functions.



<<PreviousNext>>

Looking for some other tutorial?