user1213167
user1213167

Reputation:

Mysql SELECT query calculates hours left from two dates

I have a select query. I pass today date(now()) and to date, I want my query to return no of hours there for that day.

SELECT DATEDIFF(CURDATE(), to) as timeleft

For eg: now() will be today date 16/10/2012 and to date will be 18/10/2012, I want my query to return 24 hours.

Is it possible to calculate exact hours from date, since it is almost half day over on 16/10/2012, can we get our output as 36 hours.

Upvotes: 0

Views: 809

Answers (2)

Amrita
Amrita

Reputation: 611

You can Use :

SELECT TIMESTAMPDIFF(Hour,now(),'2012-10-18');

Upvotes: 0

Geek Num 88
Geek Num 88

Reputation: 5312

When I want to calculate the difference between 2 dates I use UNIX_TIMESTAMP() to give me unix time and then calculate the seconds difference between the 2 dates.

SELECT ((UNIX_TIMESTAMP(todate) - UNIX_TIMESTAMP(fromdate))/3600) AS hoursdiff

Upvotes: 1

Related Questions