SQL PHP HTML ASP JavaScript articles and free scripts to download
 

String to Date format by str_to_date()

We have to use particular format for date & time data before storing to mysql date field. So if a user entered data is to be stored in a table then we have to change the format by using different string functions and convert them to acceptable date format. You can read this tutorial to know how the string functions are used to format the user entered data to mysql table.

By using the function str_to_date() we can convert the string data to mysql required format of date field. This function requires the string data and the format in which it is available.

str_to_date(string_data, format)

Related Tutorial
Date Formatting
Inserting Date
If the format is not supported by the string data given then the output will be NULL. For any illegal date or time string value also the output will be NULL.

Now let us try with some examples of this str_to_date function and its outputs.
SELECT STR_TO_DATE('12/15/2008', '%m/%d/%Y'); 
Output of above query is 2008-12-15

Same way here are some examples and the outputs of str_to_date function for your understanding
STR_TO_DATE('Dec/15/2008', '%M/%d/%Y');  => 2008-12-15
STR_TO_DATE('31/Jan/2008', '%d/%M/%Y');   => 2008-01-31
STR_TO_DATE( '31/Jan/2008 23:52', '%d/%M/%Y %H:%i' ) => 2008-01-31 23:52:00
Try with some illegal date
STR_TO_DATE( '32/Apr/2008', '%d/%M/%Y' ) => NULL
Learn how str_to_date is used to format date value stored as text in Varchar field of MySQL table


Further readings
Date & Time functions used in Query for MySQL Table
Getting formatted date value from date field in MySQL
date_add function to calculate date & time of MySQL table
Formatting date and time before adding to date field of MySQL
String data to Date & time Format by using str_to_date
Formatting string data stored in varchar field to date value
Automatically updating / inserting current date and time value in a DATETIME field
Records of each day by using group by command
Collecting records between two date ranges from MySQL table fields
Getting date values from MYSQL table in readable format including time
Difference in days between two date fields
Getting the year part from date field
Getting the month part from date field
Getting the day part from date field
Records of today or records of last X seconds by using curdate() or unix_timestamp
Records of last one month from today by using date field
Records of present week days by using dayofweek function
vicky01-07-2012
Thanks a lot, this has been very helpful
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

Join Our Email List
Email:  
For Email Newsletters you can trust
Date & Time
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2013 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer