使用Mysql转换日期 [英] Convert Dates with Mysql

查看:118
本文介绍了使用Mysql转换日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个格式的日期

May 30 2006 12:00 AM

May 30 2006 12:00AM

有一个等价物的strtotime()在mysql中,将让我将其转换为mysql日期?

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

推荐答案

我想你正在寻找< a href =http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date =noreferrer> STR_TO_DATE 功能。不幸的是,它不像PHP的 strtotime 那么棒,所以你必须给它一个格式掩码来解析:

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'

对于你的日期,我认为面具是%M%e%Y%l:%i%p ,具体取决于您是否期望短/长的月份名称和基于0的日期。根据你的例子,可以是:

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)

查看 full参考表用于掩码选项。

这篇关于使用Mysql转换日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆