Home  >  Article  >  Backend Development  >  Imported milk powder quality ranking PHP records are accumulated and the results are displayed with a total duration of seconds

Imported milk powder quality ranking PHP records are accumulated and the results are displayed with a total duration of seconds

WBOY
WBOYOriginal
2016-07-29 08:47:131029browse

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
indicates the duration, but the save type is text.
Now I am asking, how can I use php to accumulate these records and finally display the result as a total duration of seconds?

Copy the code The code is as follows:


//Connect to the database... slightly
$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, and 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. Then add those seconds together.
Finally get the total seconds.

The above introduces the imported milk powder quality ranking PHP records to accumulate and display the results with a total duration of seconds, including the content of the imported milk powder quality ranking. I hope it will be helpful to friends who are interested in PHP tutorials.

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