Take the 2-minute tour ×

I am creating a game using javascript stopwatch and put the results to database. The problem I am facing right now is how to calculating total time between all stages of the game. Here is my example data :

$time_level1 = "00:00:24";
$time_level2 = "00:00:30";
$time_level3 = "00:00:32";
$time_level4 = "00:01:30";
$time_level5 = "00:20:15";
$time_level6 = "00:59:15";

$time_msec_level1 = "20";
$time_msec_level2 = "343";
$time_msec_level3 = "115";
$time_msec_level4 = "952";
$time_msec_level5 = "152";
$time_msec_level6 = "455";

All those data are loaded from database. I separated the msec because on the leaderboard list I need to display all times from all stages, within the total times from all stages, which is displayed ordered by time and msec Ascending.

  1. how to calculate total times of all those variables and displaying them into time format?
  2. how to order ascending total times from multiple records? Because since the total times are not recorded inside database, I cannot order ascending instantly.
share|improve this question
Your first step should be to stop using strings to store numeric data. –  lc. Jan 2 '13 at 10:05
Hmm...you mean I should using total seconds? Like : storing 1 minutes 30 seconds as 90 seconds? Waitaminute...total secs...my bell is ringing...let me search again, will back if stuck. thanks! –  mokalovesoulmate Jan 2 '13 at 10:12
add comment

1 Answer

up vote 0 down vote accepted

Solved. What I am doing (based on lc's answer) is converting all those strings into total seconds, then convert them to display as time format. Also I can sort them ascending without problems.

Here is the final code:

$time_level1 = "220";
$time_level2 = "300";
$time_level3 = "400";
$time_level4 = "500";
$time_level5 = "600";
$time_level6 = "700";

$totaltime = $time_level1 + $time_level2 + $time_level3 + $time_level4 + $time_level5 + $time_level6;

$time_level1 = sec2hms(200);
$time_level2 = sec2hms(300);
$time_level3 = sec2hms(400);
$time_level4 = sec2hms(500);
$time_level5 = sec2hms(600);
$time_level6 = sec2hms(700);
$totaltime = sec2hms($totaltime);

echo $time_level1."<br />";
echo $time_level2."<br />";
echo $time_level3."<br />";
echo $time_level4."<br />";
echo $time_level5."<br />";
echo $time_level6."<br />";
echo $totaltime;

I use the sec2hms function from : http://www.laughing-buddha.net/php/sec2hms

share|improve this answer
add comment

Your Answer


