Exceeding MySQL Time Limit TIME 838: 59: 59

The name may be a little confusing, so let me explain. I use a table to write my work logs. Every day I create a record that indicates the time since which I worked and add a comment describing what I did.

Then I use the query to compare timestamps to find out how many hours and minutes I worked on that day. In addition, I use the query to calculate the sum of hours and minutes that I worked all year. This is where I ran into a problem. My request is as follows.

SELECT TIME_FORMAT(SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(entry_end_time, entry_start_time)))), '%H:%i') 
AS total FROM entry 
WHERE entry_date BETWEEN '2012-01-01' AND '2012-12-31' AND user_id = 3

By default, MySQL TIME fields allow you to use the time range from -838: 59: 59 'to' 838: 59: 59 '. Currently, I have registered more than 900 hours of work this year, and I want the result of my query to reflect this. Instead, the result is 838: 59: 59, which makes sense because it's the limit.

Is there a way around this so the query result can go beyond 839 hours, or will I need to use something like PHP to go through the whole table and add all this? I would like to avoid this, if possible.

+6
source share
7 answers

I would just get the total number of working seconds and convert to hours / minutes, as required at the presentation level of my application (this is, after all, a simple case of dividing by 60):

<?
  $dbh = new PDO("mysql:dbname=$dbname", $username, $password);
  $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);

  $qry = $dbh->prepare('
    SELECT SUM(TIME_TO_SEC(entry_end_time)-TIME_TO_SEC(entry_start_time))
    FROM   entry 
    WHERE  entry_date BETWEEN :start_date AND :end_date
       AND user_id = :user_id
  ');

  $qry->execute([
    ':start_date' => '2012-01-01',
    ':end_date'   => '2012-12-31',
    ':user_id'    => 3
  ]);

  list ($totalMins, $remngSecs) = gmp_div_qr($qry->fetchColumn(), 60);
  list ($totalHour, $remngMins) = gmp_div_qr($totalMins, 60);

  echo "Worked a total of $totalHour:$remngMins:$remngSecs.";
?>
+6

timestampdiff, TIME. - ():

SELECT CONCAT(
        TIMESTAMPDIFF(HOURS, entry_end_time, entry_start_time), 
        ":",
        MOD(TIMESTAMPDIFF(MINUTES, entry_end_time, entry_start_time),60)
      )
AS total FROM entry 
WHERE entry_date BETWEEN '2012-01-01' AND '2012-12-31' AND user_id = 3

, , .

+3

- , (10000000)

SELECT CONCAT(FLOOR(10000000/3600),':',FLOOR((10000000%3600)/60),':',(10000000%3600)%60)

2777:46:40
+1

.
, .

/ , , (TIME format max value)
.

SELECT 'pq7~' AS unicorn
#######################
##  Expected result  ##
#######################
## Total, formatted as days:hh:mm:ss ##
  ,CONCAT(
    FLOOR(TIMESTAMPDIFF(SECOND, '2017-01-01 09:17:45', '2017-03-07 17:06:24') / 86400)
    , ':'
    , SEC_TO_TIME(TIMESTAMPDIFF(SECOND, '2017-01-01 09:17:45', '2017-03-07 17:06:24') % 86400)
  ) AS Real_expected_result

#########################
## Calculation details ##
#########################
## Extracted days from diff ##
  ,FLOOR(TIMESTAMPDIFF(SECOND, '2017-01-01 09:17:45', '2017-03-07 17:06:24') / 86400) AS Real_days
## Extracted Hours/minutes/seconds from diff ##
  ,SEC_TO_TIME(TIMESTAMPDIFF(SECOND, '2017-01-01 09:17:45', '2017-03-07 17:06:24') % 86400) AS Real_hours_minutes_seconds

###################################
## Demo of wrong values returned ##
###################################
  ,TIMESTAMPDIFF(SECOND, '2017-01-01 09:17:45', '2017-03-07 17:06:24') AS Real_seconds_diff

## WRONG value returned. 5.64M is truncated to 3.02 ! ##
  ,TIME_TO_SEC(SEC_TO_TIME(5644119)) AS WRONG_result

## Result is effectively limited to 838h59m59s ##
  ,SEC_TO_TIME(TIMESTAMPDIFF(SECOND, '2017-01-01 09:17:45', '2017-03-07 17:06:24')) AS Limit_hit

## Lights on said limit ##
  ,SEC_TO_TIME( 3020398) AS Limit_value_check1
  ,SEC_TO_TIME( 3020400) AS Limit_value_check2
0

First, calculate the difference in days, then multiply it by 24 * 60 * 60 to convert it to seconds, and then add the result of the value time_to_sec to it

DATEDIFF(start_date,end_date)*24*60*60 + TIME_TO_SEC(TIMEDIFF(TIME(start_date),TIME(end_date))) 
AS sec_diff

For more details see codebucket - exceeding the maximum time of the time_to_sec () function

-1
source
select  concat(truncate(sum(time_to_sec(TIMEDIFF(hora_fim, hora_ini)))/3600,0), ':', 
TIME_FORMAT(sec_to_time(sum(time_to_sec(TIMEDIFF(hora_fim, hora_ini))) - truncate(sum(time_to_sec(TIMEDIFF(hora_fim, hora_ini)))/3600,0)*3600), '%i:%s'))
as hms from  tb_XXXXXX
-1
source

All Articles