php 记录进行累加并显示总时长为秒的结果
现在有一个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函数,以“:”分割字符串,得到一个数组。
然后分别算出小时对应的秒数,分钟对应的秒数。然后和把这些秒数加起来。
最后得到总秒数。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool