mysql str_to_date 字符串转日期类型

来自http://tunps.com/mysql-str_to_date

酝酿

最近写的那个嘀咕备份程序,有个需要修改的地方。 首先来看看嘀咕api生成的任意一个含有<status>的xml文件 http://api.minicloud.com.cn/statuses/public_timeline.xml 。 里面的这个节点:<created_at>Mon Mar 15 02:01:51 +0800 2010</created_at> created_at节点的意思是 这个元素创建的UTC时间戳。via 恕我无知,我确实不知道这个时间戳格式弄成这样有啥好处:
Sat May 02 20:32:02 +0800 2009
星期 月  日  时:分:秒  时区   年
所以开始在写表结构的时候,把这个字段用varchar来表示,这样的缺点就是无法对所有的嘀咕消息通过时间条件来查询。 其实还有一个缺点:http://api.minicloud.com.cn/statuses/user_timeline.xml?userIdOrName=tunpishuang&page=1 。 我写的代码是从第一页(page=1)一直到最后一页,page=1是最新的消息,也就是最新的消息先写入表中,这些记录的主键id=1,2,3,4…….这无非是一个order by id asc or desc的一问题,但是我以后还要通过其他的渠道向表插入新纪录,这样就混乱了timeline。 所有想扯了这么远,回归到正题就是将这个created_at的值转换成mysql能够识别的日期格式。

正题

mysql手册上发现了我们需要的函数str_to_date  http://dev.mysql.com/doc/refman/5.1/zh/functions.html
mysql> select str_to_date('Sat May 02 20:32:02 +0800 2009','%a %b %e %k:%i:%s +0800 %Y');
+----------------------------------------------------------------------------+
| str_to_date('Sat May 02 20:32:02 +0800 2009','%a %b %e %k:%i:%s +0800 %Y') |
+----------------------------------------------------------------------------+
| 2009-05-02 20:32:02                                                        |
+----------------------------------------------------------------------------+
1 row in set

mysql>
我发现我写东西都喜欢循循善诱,看来我很适合当老师。

About tunpishuang

just 4 fun·····
This entry was posted in 未分类 and tagged . Bookmark the permalink.

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>