An alternative solution to this problem is to write a custom function. Keep in mind that the return value is no longer a time object, but a string. Therefore, in MySQL you cannot use it for further calculation:
DELIMITER $$ DROP FUNCTION IF EXISTS `big_time_diff`$$ CREATE DEFINER=`root`@`localhost` FUNCTION `big_timediff`(s_start VARCHAR(20), s_end VARCHAR(20)) RETURNS TEXT CHARSET latin1 BEGIN DECLARE s_hour VARCHAR(20); DECLARE s_rest VARCHAR(20); SET s_hour = ABS(TIMESTAMPDIFF(HOUR, s_end, s_start)); SET s_rest = TIME_FORMAT(TIMEDIFF(s_start, s_end), ':%i:%s'); RETURN INSERT(s_rest, LOCATE('-', s_rest) + 1, 0, IF(LENGTH(s_hour) > 2, s_hour, LPAD(s_hour, 2, 0))); END$$ DELIMITER ;
jmmeier
source share