Reputation:
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
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