Surpassing MySQL's TIME value limit of 838:59:59

前端 未结 6 541
南旧
南旧 2020-12-01 14:54

The title might be a bit confusing so allow me to explain. I\'m using a table to record my work logs. Every day I\'ll create an entry stating from what time to what time I h

6条回答
  •  攒了一身酷
    2020-12-01 15:36

    I'd just retrieve the total number of seconds worked, and convert to hours/minutes as required in the presentation layer of my application (it is, after all, a simple case of division by 60):

    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.";
    ?>
    

提交回复
热议问题