Pls help me to add two times which is in mysql database table....Very urgent...Thank you

Dani AI

Generated

Two practical approaches for adding hh:mm:ss values were suggested in the thread: do it in MySQL (as recommended) or use date functions on the application side (as noted). For single-row addition the built-in MySQL time functions are convenient; for totals across many rows or when values are stored as text, converting to seconds, summing, then converting back is more robust and avoids surprises with NULLs or mixed formats.

A reliable SQL pattern for totals is to convert each time to seconds, sum those integers, and convert back to hh:mm:ss. For example:

SELECT SEC_TO_TIME(SUM(COALESCE(TIME_TO_SEC(duration_col),0))) AS total_time
FROM work_log;

This handles NULL values and produces a duration that can exceed 24 hours (MySQL TIME supports up to +/-838:59:59).

If the addition is done in PHP (fetch times and compute), using simple parsing to seconds is fast and version-independent. For example:

$total = 0;
foreach ($rows as $r) {
  list($h,$m,$s) = array_map('intval', explode(':', $r['time']));
  $total += $h*3600 + $m*60 + $s;
}
$sum = sprintf('%02d:%02d:%02d',
    floor($total/3600),
    floor(($total%3600)/60),
    $total%60
);

Troubleshooting notes: ensure the DB column is a TIME type or cast strings with CAST(... AS TIME) or STR_TO_DATE(...) before converting; use COALESCE to avoid NULL propagation; beware of nonstandard formats like "H:M" or missing seconds. Official references: MySQL date/time functions (MySQL docs) and PHP DateTime/interval handling (PHP manual).

Recommended Answers

All 2 Replies

you can do it as select it from the mysql side

select time1, time2, addtime(time1,time2)
from table
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.