计算多个变量的总小时数,分钟数和秒数

时间:2013-01-02 10:03:33

标签: php time

我正在使用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";

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

  1. 如何计算所有这些变量的总时间并将其显示为时间格式?
  2. 如何从多个记录中订购增加的总时间?因为数据库中没有记录总时间,所以我无法立即命令升序。

1 个答案:

答案 0 :(得分: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;
?>

我使用http://www.laughing-buddha.net/php/sec2hms

中的sec2hms函数