Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL function reaches a limit in time difference calculation

Tags:

datetime

mysql

I've created a function for mysql using phpmyadmin (that works fine). It calculates de diference between two dates in days, hours, minutes and seconds. The output could be one like this:

5d 16h 24m 56s

The mentioned function:

CREATE FUNCTION time_full(A DATETIME, B DATETIME) RETURNS  VARCHAR(30)
BEGIN

RETURN CONCAT(FLOOR(HOUR(TIMEDIFF(A,B))/24),'d '
               ,MOD(HOUR(TIMEDIFF(A,B)),24), 'h '
               ,MINUTE(TIMEDIFF(A,B)),'m '
               ,SECOND(TIMEDIFF(A, B)),'s');

END

So after creating it and checking that works fine I decided to make it calculate a big gap of time between two dates, something like 2 months or so, but the biggest gap that it can calculates is:

34d 22h 59m 59s

Anyone knows why? (That's the big question)

I knew something was happening since with the query function:

SEC_TO_TIME(TIMESTAMPDIFF(SECOND, created, sol_provided)) AS timeDiff

I was having a trouble since the biggest gap in hours that it could calculate was something like 848 hours or so.

like image 907
ederollora Avatar asked Oct 16 '25 14:10

ederollora


1 Answers

From the MySQL website (http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff)

The result returned by TIMEDIFF() is limited to the range allowed for TIME values

Then if you continue to the section on TIME, (http://dev.mysql.com/doc/refman/5.5/en/time.html)

TIME values may range from '-838:59:59' to '838:59:59'. The hours part may be so large because the TIME type can be used not only to represent a time of day (which must be less than 24 hours), but also elapsed time or a time interval between two events (which may be much greater than 24 hours, or even negative).

like image 174
AgRizzo Avatar answered Oct 19 '25 04:10

AgRizzo