如何将数据类型varchar转换为时间戳mysql?

时间:2014-01-30 05:46:18

标签: mysql sql datetime select

我有数据库mysql,表'tblevel',列'waktu',varchar数据类型。 并且列保持如下值:

30-01-2014 12:19:09

该值将转换为时间戳,因为我必须在图形中查看数据,在这种情况下我使用谷歌数据可视化,

短代码是:

$result = mysql_query("SELECT TIMESTAMPDIFF(MINUTE,FROM_UNIX_TIMESTAMP(STR_TO_DATE(waktu, '%H:%i:%s %b %d, %Y')), now()) as waktu1,level,press FROM tblevel WHERE TIMESTAMPDIFF(MINUTE,FROM_UNIXTIME(waktu) , now()) < 120 Order By waktu"); 

希望有人帮助纠正代码。谢谢你 关于Fixduino。

2 个答案:

答案 0 :(得分:0)

试试这个:

SELECT a.waktu, a.level, a.press 
       TIMESTAMPDIFF(MINUTE, STR_TO_DATE(a.waktu, '%d-%m-%Y %H:%i:%s'), NOW()) AS waktu1           
FROM tblevel a
WHERE TIMESTAMPDIFF(MINUTE, STR_TO_DATE(a.waktu, '%d-%m-%Y %H:%i:%s'), NOW()) < 120 
ORDER BY STR_TO_DATE(a.waktu, '%d-%m-%Y %H:%i:%s'); 

答案 1 :(得分:0)

MySQL的日期格式是YMD,试试这个

SELECT a.waktu, a.level, a.press 
       TIMESTAMPDIFF(MINUTE, STR_TO_DATE(a.waktu, '%y-%M-%d %H:%i:%s'), NOW()) AS waktu1           
FROM tblevel a
WHERE TIMESTAMPDIFF(MINUTE, STR_TO_DATE(a.waktu, '%y-%M-%d %H:%i:%s'), NOW()) < 120 
ORDER BY STR_TO_DATE(a.waktu, '%y-%M-%d %H:%i:%s');