为什么我的MySQL TIMESTAMPDIFF不起作用?

时间:2015-03-25 23:38:43

标签: php mysql timestamp timedelta

你好我有一个存储的时间戳"旧时"我想从现在的时间中得到差异,但它不起作用,也没有抛出任何错误,所以我迷失了。

$timediff = mysql_query(sprintf("SELECT TIMESTAMPDIFF(SECOND, %s, NOW())", GetSQLValueString($oldtime, "text")), $DBName) or die (mysql_error());

$diff = mysql_fetch_assoc($timediff);

echo '<script>console.log("time diff:'.$diff[0].'");</script>';

.$diff. results in "time diff:Array"

.$diff[0]. results in "time diff: "

.$timediff. results in "time diff:Resource id #6"

1 个答案:

答案 0 :(得分:0)

默认情况下,mysql_fetch_assoc返回一个按列名索引的数组,而不是列号。

列名称实际上是函数字符串,类似于

TIMESTAMPDIFF(table.col1, NOW())

这里更简单的方法是使用mysql_fetch_row或mysql_fetch_array

来取代列号

请参阅: http://php.net/manual/en/function.mysql-fetch-array.php http://php.net/manual/en/function.mysql-fetch-array.php

另一种方法是使用一些数组函数来提取没有名称的元素,参见: http://php.net/manual/en/ref.array.php