0

我正在使用 javascript 秒表创建游戏并将结果放入数据库。我现在面临的问题是如何计算游戏所有阶段之间的总时间。这是我的示例数据:

$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";

所有这些数据都是从数据库加载的。我将毫秒分开是因为在排行榜列表中我需要显示所有阶段的所有时间,在所有阶段的总时间内,按时间和毫秒升序显示。

  1. 如何计算所有这些变量的总时间并将它们显示为时间格式?
  2. 如何从多条记录中按升序排列总时间?因为数据库中没有记录总次数,所以我不能立即升序。
4

1 回答 1

0

解决了。我正在做的(基于 lc 的回答)是将所有这些字符串转换为总秒数,然后将它们转换为时间格式。我也可以毫无问题地对它们进行升序排序。

这是最终代码:

<?php
$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;
?>

我使用的sec2hms功能来自:http ://www.laughing-buddha.net/php/sec2hms

于 2013-01-02T10:37:46.753 回答