PHP strtotime():转换后显示'1970-01-01'的日期

时间:2018-05-04 12:14:18

标签: php mysql datetime

我正在从Excel工作表中读取数据并插入MySQL表。在此过程中,我使用PHP方法{ title: 'dcdcsd', content: '<p>Here You Can Add Content</p>\r\n' } { Error: ER_BAD_FIELD_ERROR: Unknown column 'title' in 'field list' at Query.Sequence._packetToError (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14) at Query.ErrorPacket (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/mysql/lib/protocol/sequences/Query.js:77:18) at Protocol._parsePacket (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/mysql/lib/protocol/Protocol.js:279:23) at Parser.write (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/mysql/lib/protocol/Parser.js:76:12) at Protocol.write (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/mysql/lib/protocol/Protocol.js:39:16) at Socket.<anonymous> (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/mysql/lib/Connection.js:103:28) at emitOne (events.js:116:13) at Socket.emit (events.js:211:7) at addChunk (_stream_readable.js:263:12) at readableAddChunk (_stream_readable.js:250:11) -------------------- at Protocol._enqueue (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/mysql/lib/protocol/Protocol.js:145:48) at Connection.query (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/mysql/lib/Connection.js:208:25) at Router.post (/media/gaurav/301286221285ED62/NEW DESIGN/routes/adminRoute.js:325:20) at Layer.handle [as handle_request] (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/express/lib/router/layer.js:95:5) at next (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/express/lib/router/layer.js:95:5) at /media/gaurav/301286221285ED62/NEW DESIGN/node_modules/express/lib/router/index.js:281:22 at Function.process_params (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/express/lib/router/index.js:335:12) at next (/media/gaurav/301286221285ED62/NEW DESIGN/node_modules/express/lib/router/index.js:275:10) code: 'ER_BAD_FIELD_ERROR', errno: 1054, sqlMessage: 'Unknown column \'title\' in \'field list\'', sqlState: '42S22', index: 0, sql: 'INSERT INTO bwa_post(category_id,title, content, featured, slug, created_by) SELECT id, `title` = \'dcdcsd\',`content` = \'<p>Here You Can Add Content</p>\\r\\n\', `featured` = \'dscsdcsd\', `slug` = \'dcdcsd\', `created_by` = \'Gaurav\' FROM bwa_category WHERE category_name = \'fakedata\'' } 将时间戳(字符串)转换为datetime。

strtotime()

在某些情况下失败了。

示例:

$timestamp = date('Y-m-d H:i:sa',strtotime(trim($value->Time)));

输出:

 echo date('Y-m-d H:i:s',strtotime('04-13-2018 0:00:53'));

 echo date('Y-m-d H:i:s',strtotime('04-12-2018 0:00:53'));

任何人都可以帮助我如何解决这个问题?

失败的示例字符串:

1970-01-01 00:00:00

2018-12-04 00:00:53

3 个答案:

答案 0 :(得分:7)

您的格式不是format that the parser understands

在您的情况下13不是&#34;月&#34;。所以解析器到目前为止还没有理解。

您应该使用DateTime::createFromFormat()

$date = DateTime::createFromFormat('m-d-Y H:i:s','04-13-2018 0:00:53');
echo $date->format('Y-m-d H:i:s');

输出:

2018-04-13 00:00:53

请注意,格式也可以是'm-d-Y G:i:s',其中G代表&#34; 24小时格式的小时不带前导零&#34;

答案 1 :(得分:1)

添加已经回答的内容 您获得1970年的原因是strtotime如果无法解析日期,则返回false。
0(假)unix时间是1970年。

答案 2 :(得分:1)

strtotime()的字符串格式错误(我不记得技术术语)&#34;美式格式&#34;值:

echo date('Y-m-d H:i:s', strtotime('04-13-2018 0:00:53'));
echo date('Y-m-d H:i:s', strtotime('04/13/2018 0:00:53'));

https://3v4l.org/Qnsru

给出:

1970-01-01 01:00:00
2018-04-13 00:00:53

基本上,你给了strtotime()(它叫什么?)&#34;欧洲约会时间&#34;值。表示-是第一部分,而不是

很久以前我就这样了。