I've been busy with this for hours, but I cant get it to work.
SQL SELECT DATEDIFF(end_time, start_time) as `difference` FROM timeattendance WHERE timeattendance_id = '1484' start_time = 2012-01-01 12:00:00 end_time = 2012-01-02 13:00:00
The difference is 25 (hours). But the output I get is 1 (day).
How can I get the 25 hours as output?
To calculate the difference between the timestamps in MySQL, use the TIMESTAMPDIFF(unit, start, end) function. The unit argument can be MICROSECOND , SECOND , MINUTE , HOUR , DAY , WEEK , MONTH , QUARTER , or YEAR .
Discussion: To count the difference between dates in MySQL, use the DATEDIFF(enddate, startdate) function. The difference between startdate and enddate is expressed in days. In this case, the enddate is arrival and the startdate is departure .
The DATEDIFF function can be used in the following versions of MySQL: MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1.
To compute the total elapsed time, use TIME_TO_SEC( ) to convert the values to seconds before summing them. : TIME_TO_SEC « Date Time « SQL / MySQL. To compute the total elapsed time, use TIME_TO_SEC( ) to convert the values to seconds before summing them.
What about using TIMESTAMPDIFF?
SELECT TIMESTAMPDIFF(HOUR, start_time, end_time) as `difference` FROM timeattendance WHERE timeattendance_id = '1484'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With