Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Dates with Mysql

Tags:

datetime

mysql

i have a date in this format

May 30 2006 12:00AM

is there a equivalent of strtotime() in mysql that will let me convert this to a mysql date?

like image 316
gsueagle2008 Avatar asked Apr 08 '09 19:04

gsueagle2008


People also ask

How do I change date format from YYYY-MM-DD in MySQL?

mysql> select *from ConvertIntoDateFormat; The following is the output. The following is the query to format the date to YYYY-MM-DD. mysql> select str_to_date(LoginDate,'%d.

Can we change date format in MySQL?

Change the curdate() (current date) format in MySQL The current date format is 'YYYY-mm-dd'. To change current date format, you can use date_format().

How do I convert a date to a string in MySQL?

In MySQL, DATE_FORMAT function converts a DATE or DATETIME value to string using the specified format. In Oracle, you can use TO_CHAR function. Note that the DATE_FORMAT and TO_CHAR use different format strings.

What is the date format in MySQL?

MySQL retrieves and displays DATE values in ' YYYY-MM-DD ' format. The supported range is '1000-01-01' to '9999-12-31' . The DATETIME type is used for values that contain both date and time parts.


1 Answers

I think you are looking for the STR_TO_DATE function. Unfortunately, it is not quite as awesome as PHP's strtotime, so you have to give it a format mask to parse:

mysql> SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y');
        -> '2004-04-31'

For your date, I think the mask would be %M %e %Y %l:%i%p, depending on whether or not you are expecting short/long month names and 0-based days. Based on your example it could be either:

mysql> SELECT STR_TO_DATE('May 30 2006 12:00AM', '%M %e %Y %l:%i%p');
+--------------------------------------------------------+
| STR_TO_DATE('May 30 2006 12:00AM', '%M %e %Y %l:%i%p') |
+--------------------------------------------------------+
| 2006-05-30 00:00:00                                    |
+--------------------------------------------------------+
1 row in set (0.00 sec)

Check out the full reference table for the mask options.

like image 169
Paolo Bergantino Avatar answered Oct 22 '22 13:10

Paolo Bergantino