PHP:无法将MySQL DateTime转换为纪元(使用正确的时间)


PHP: Can't Convert MySQL DateTime to Epoch (with Correct Time of Day)

我正在使用php从MySQL数据库中获取日期时间。

在mysql中,日期时间如下所示:

'
2004-11-03 06:29:48'

从数据库中获取日期时间的代码行如下所示:

$epochTime = strtotime($row[8]);

PHP 的 strtotime 函数将此时间转换为错误的纪元时间:

1099461600

这是不正确的。如果您使用 http://www.epochconverter.com/将其转换回来,您可以看到最初在 mysql 中的一天中的时间已经丢失:

周三, 03 十一月 2004 06:00:00 GMT

如何从 mysql 获得具有与日期时间匹配的特异性的准确纪元时间?

编辑:

我刚刚发现mysql只返回日期(没有时间):

2004/11/03

我试图使用它强制它返回时间:

select DATE_FORMAT(`FieldDateTime`,'%Y-%m-%d %k:%I:%s') from table where id=1;

但这也没有用;它继续只返回日期(没有时间)。

咳!我想通了。我在数组中指定了错误的字段:

$epochTime = strtotime($row[8]);

应该是:

$epochTime = strtotime($row[9]);

事实证明,$row[8]也是一个格式化的日期字段,引起了我的困惑。感谢您的帮助!

你可以试试:

SELECT UNIX_TIMESTAMP(`timestamp_column_name`) AS `timestamp`;

然后在 PHP 中:

echo date("Y-m-d H:i:s", $row['timestamp']);

这是否与 MySQL 匹配?