Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

datetime - Convert Dates with 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...