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

时间:2022-01-12 11:27:02

I'm grabbing a datetime from a MySQL database using php.

我正在使用php从MySQL数据库中获取一个datetime。

In mysql the datetime looks like this:

在mysql中,datetime如下所示:

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

The line of my code that get the datetime from the database looks like this:

从数据库获取datetime的代码如下所示:

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

Php's strtotime function convert this time to the wrong epoch time:

Php的strtotime函数将这个时间转换为错误的纪元时间:

1099461600

That's incorrect. If you convert this back using http://www.epochconverter.com/ you can see that the time of day that was originally in mysql has been lost:

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

Wed, 03 Nov 2004 06:00:00 GMT

How can I get an accurate epoch time that has a specificity that matches the datetime from mysql?

如何获得一个精确的历元时间,它具有与mysql的datetime匹配的特性?

Edits:

编辑:

I just discovered that mysql is only returning the date (without the time):

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

2004/11/03

I've tried to force it return the time using this:

我试图强迫它用这个来返回时间:

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

But this didn't work either; it continues to only return the date (without the time).

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

Ack! I figured it out. I was specifying the wrong field in my array:

Ack !我想出来。我在数组中指定了错误的字段:

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

Should have been:

应该是:

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

It turns out that $row[8] was a formatted date field too, causing my confusion. Thanks for the help!

原来$row[8]也是一个格式化的日期字段,这让我很困惑。谢谢你的帮助!

1 个解决方案

#1


3  

You could try:

你可以试试:

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

Then in PHP:

然后在PHP中:

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

Does this match MySQL?

这场比赛MySQL吗?

#1


3  

You could try:

你可以试试:

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

Then in PHP:

然后在PHP中:

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

Does this match MySQL?

这场比赛MySQL吗?