PHP strtotime():转换后显示"1970-01-01"的日期 [英] PHP strtotime(): date showing '1970-01-01 ' after conversion

查看:644
本文介绍了PHP strtotime():转换后显示"1970-01-01"的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Excel工作表中读取数据并将其插入MySQL表中.在此过程中,我将使用PHP方法strtotime()将时间戳(字符串)转换为日期时间.

I am reading the data from an excel sheet and inserting into MySQL table. In this process, I am converting the timestamp (string) to datetime using the PHP method strtotime().

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

在某些情况下,此操作将失败.

This is failing in some cases.

示例:

 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

有人可以帮我解决这个问题吗?

Can anyone help me how can I solve this issue?

失败的示例字符串:

04-12-2018 0:00:53
04-12-2018 0:01:53
04-12-2018 0:02:53
04-12-2018 0:03:53
04-12-2018 0:04:53
04-12-2018 0:05:53
04-12-2018 0:06:53
04-12-2018 0:07:53
04-12-2018 0:08:54
04-12-2018 0:09:54
04-12-2018 0:10:53
04-12-2018 0:11:53
04-12-2018 0:12:53
04-12-2018 0:13:53
04-12-2018 0:14:53
04-12-2018 0:15:53
04-12-2018 0:16:53
04-12-2018 0:17:53
04-12-2018 0:18:53
04-12-2018 0:19:54
04-12-2018 0:20:54
04-12-2018 0:21:54
04-12-2018 0:22:53
04-12-2018 0:23:54
04-12-2018 0:24:53
04-12-2018 0:25:54

推荐答案

您的格式不是解析器可以理解的格式.

在您的情况下,13不是月".因此解析器到目前为止还不了解.

In your case 13 is not a "month". So the parser doesn't understand to date.

您应使用 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表示一个小时的24小时格式,不带前导零" .

Note that the format could also be: 'm-d-Y G:i:s' with G for "24-hour format of an hour without leading zeros".

这篇关于PHP strtotime():转换后显示"1970-01-01"的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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