我的数据库有一个TIMESTAMP类型的数据字段。它被称为“mytime”。我从php的time()函数中得到一个数字。 e.g:
print time();
然后我查询db:
SELECT * FROM mytable where mytime > 1369157557
它抱怨道:
conversion error from string "1369157557"
我猜你无法将PHP时间戳与Firebird时间戳进行比较。为什么不?我该如何解决它?
答案 0 :(得分:3)
有两种选择:在比较之前将该unix时间戳转换为Firebird时间戳。或者执行this message on the Firebird-Support list中描述的内容:
select DATEDIFF(second, timestamp '1/1/1970 00:00:00', current_timestamp)
from rdb$database
或者特别是在您的查询中:
SELECT *
FROM mytable
where DATEDIFF(second, timestamp '1/1/1970 00:00:00', mytime) > 1369157557
请记住,存储在数据库中的时间可能不会以UTC格式存储,在这种情况下,您还需要考虑对UTC的偏移量。
答案 1 :(得分:0)
您可以使用UNIX_TIMESTAMP函数。请注意,尽管php和mysql可能不同意,但是时区方面也只有几秒钟。
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp
SELECT * FROM mytable where UNIX_TIMESTAMP(mytime) > 1369157557