Home > php教程 > php手册 > body text

php 记录进行累加并显示总时长为秒的结果

WBOY
Release: 2016-06-13 12:04:48
Original
754 people have browsed it

现在有一个mysql数据库的test表里有一个duration字段,里面有三条记录:
00:22:32
13:42:21
134:42:21

表示的是时长,但是,保存类型是文本。

现在要求,用php如何将这些记录进行累加,最后显示为一个总时长为秒钟的结果?

复制代码 代码如下:


//连接数据库... 略
$total = 0; //总秒数
$sql = "select duration from test";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
$arr=explode(":",$row[duration]);
$h = $arr[0]*60*60;
$m = $arr[1]*60;
$s = $arr[2];
$total = $h+$m+$s;
}
echo $total;



这里主要是查询出数据,然后使用explode函数,以“:”分割字符串,得到一个数组。
然后分别算出小时对应的秒数,分钟对应的秒数。然后和把这些秒数加起来。
最后得到总秒数。
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!