CSV到Mysql插入日期时间 [英] CSV to Mysql insert datetime

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

问题描述

我有CSV文件,我想将CSV行插入Mysql数据库. 当我在开始日期"(格式为日期时间")列中从CSV将"23.05.17 17:53:25"从CSV插入到DB时,我得到2023-25-17 17:53:25.

I have CSV file and i want insert CSV rows to Mysql database. When I inserting "23.05.17 17:53:25" to DB from CSV in column START DATE(format Datetime), I am getting 2023-25-17 17:53:25.

我该如何转换,这在数据库上会正确获取日期格式2017-05-23 17:53:25

How i can convert that, which on DB get correctly date format 2017-05-23 17:53:25

谢谢

推荐答案

它可能不是正统的解决方案,但是您可以简单地解析输入字符串并以mysql期望的格式重新创建日期时间.

It might not be an orthodox solution, but you could simply parse the input string and recreate the datetime in the format mysql expects.

// for each line in the csv
list($datePart, $timePart) = explode(' ', $fields['date']);
list($d, $m, $y) = explode('.', $datePart);
$dbDate = "$y-$m-$d $timePart";

最好添加一些输入验证,但是如果您正在做的是数据导入脚本,就可以了.

It would be better to add some input validation, but if what you're doing is a data import script it's ok.

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

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