Home  >  Article  >  Backend Development  >  PHP records are accumulated and the total duration is displayed in seconds_PHP Tutorial

PHP records are accumulated and the total duration is displayed in seconds_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:23:46946browse

Now there is a duration field in the test table of a mysql database, which contains three records:
00:22:32
13:42:21
134:42:21

means is the duration, but the save type is text.

Now, how can I use PHP to accumulate these records and finally display the result as a total duration of seconds?

Copy code The code is as follows:

//Connect to the database... Omitted
$total = 0; // Total seconds
$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;


The main purpose here is to query the data. Then use the explode function to split the string with ":" to get an array.
Then calculate the number of seconds corresponding to the hour and the number of seconds corresponding to the minute respectively. Then add those seconds together.
Finally get the total seconds.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324432.htmlTechArticleThere is now a duration field in the test table of a mysql database with three records in it: 00:22:32 13:42:21 134:42:21 indicates the duration, but the saving type is text. Now to...
Statement:
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