Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting minute difference between two timestamps in mysql using TIMESTAMPDIFF

Tags:

php

mysql

I have my SQL statement like this trying to get the difference in 2 timestamps greater than 10 minutes. "timestamp" is a column in MYSQL which I hold a timstamp as such "1365793346"

SELECT * FROM table WHERE TIMESTAMPDIFF(MINUTE,timestamp,NOW()) AS thisisit

Im not sure if using "AS thisisit" is a current function of TIMESTAMPDIFF but I was able to find some old posts that how it used as such. I am not sure if its supported anymore because I an a syntax error at "AS thisisit"

I have also tried using

SELECT * FROM table WHERE TIMESTAMPDIFF(MINUTE,timestamp,NOW()) > 10

Where I am not sure what is going on is first is my syntax correct and second how to do associate this query with a label so I can echo it. My full PhP code looks like this

SELECT * FROM table WHERE TIMESTAMPDIFF(MINUTE,timestamp,NOW()) > 10
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo $row[0];
}

I was assuming I could use something like this to echo the results, but I get nothing to the screen. Can anyone point me in the right direction?

echo $row[0];
like image 268
Cesar Bielich Avatar asked Apr 12 '13 23:04

Cesar Bielich


People also ask

How do you find the difference in time between two timestamps?

The difference is calculated by subtracting the second operand from the first. The result is rounded down, with any remainder discarded. For example, 61 minutes is equal to 1 hour, and 59 minutes is equal to 0 hours.

What does Timestampdiff mean in SQL?

Calculates the difference between two date, time, or timestamp expressions based on the specified date or time part. The function returns the result of subtracting the second argument from the third argument.

What does Timestampdiff return?

TIMESTAMPDIFF() : This function in MySQL is used to return a value after subtracting a DateTime expression from another.

What is the difference between datediff and Timestampdiff?

So both functions return the difference in days, however one result is positive and the other negative. This is because DATEDIFF() subtracts the second date from the first, whereas TIMESTAMPDIFF() subtracts the first date from the second.


1 Answers

AS thisisit in this case have to be used to set an alias to your column.

So, you should use the following:

SELECT timestamp AS 'thisisit'
    FROM table
WHERE TIMESTAMPDIFF(MINUTE, timestamp, NOW()) > 10;
like image 123
Ricardo Alvaro Lohmann Avatar answered Oct 03 '22 15:10

Ricardo Alvaro Lohmann