Hello Friends..
there is a time Duration field in my page..!!
in format
mins:sec
03:45
05:11
.
.
i want o show total of all time duration in last...!!
.
.
is there any predefined function to do this..!!
.
.
thanks..!! :)

Dani AI

Generated

For : the reliable, easiest strategy is to convert each MM:SS string to seconds, add them, then convert the total back into minutes (or MM:SS) for display. 's session/JS suggestion is useful for a live timer, but not needed when you just want to total stored durations.

A straightforward PHP approach (robust for MM:SS and HH:MM:SS) — parse, sum seconds, then format:

$totalSeconds = 0;
foreach ($rows as $row) {
    $parts = explode(':', trim($row['duration']));
    if (count($parts) == 2) {
        list($m, $s) = array_map('intval', $parts);
        $totalSeconds += $m*60 + $s;
    } elseif (count($parts) == 3) {
        list($h, $m, $s) = array_map('intval', $parts);
        $totalSeconds += $h*3600 + $m*60 + $s;
    }
}
$totalMinutes = floor($totalSeconds / 60);
$display = sprintf('%d:%02d', $totalMinutes, $totalSeconds % 60);

If values are in MySQL, aggregate on the database side (faster for many rows). For MM:SS strings use CONCAT('00:', ...) so MySQL treats them as HH:MM:SS:

SELECT FLOOR(SUM(TIME_TO_SEC(CONCAT('00:', duration_column))) / 60) AS total_minutes
FROM mytable;

-- or to get HH:MM:SS total:
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(CONCAT('00:', duration_column)))) AS total_time
FROM mytable;

Validation and tips: normalize inputs (trim, force two-digit seconds), validate with a regex like /^\d+:[0-5]\d$/, and handle NULLs. If you can change the schema, store durations as integer seconds — summing an INT column is simplest and avoids repeated parsing. For PHP parsing basics see explode(). For MySQL time functions see Date and Time Functions.

Recommended Answers

All 2 Replies

Member Avatar for Member #120589

Hello Friends..
there is a time Duration field in my page..!!
in format
mins:sec
03:45
05:11
.
.
i want o show total of all time duration in last...!!
.
.
is there any predefined function to do this..!!
.
.
thanks..!! :)

Do you want this like a clock to continually update or just when a new page is loaded?

php could do the latter without using javascript via a cookie or session variable or even through a database value

In every page:

session_start();
//set the current time in seconds - unix timestamp
$now = time();

//set the session variable if not already set
if(!isset($_SESSION['start']))$_SESSION['start'] = $now;


//this will give you the number of seconds since the first page was accessed
$diff = $now - $_SESSION['start'];

//you can now do the simple arithmetic yourself.

if you wanted to do the former, i'd suggest using javascript and not really bothering with php at all.

there is a field in named 'Time Duration' in my table..!! all i just want to calculate total Duration in end..!!! and it must be in minutes..!!! there are logics on which i worked... but still i m lookin for any php function through which i can do easily..!! :)

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.