The php record is accumulated and the result of the total length of time is displayed in seconds.
Source: Internet
Author: User
How to accumulate these records using php, and finally display the result as a result of a total length of seconds. now there is a duration field in the test table of mysql database, which has three records:
00:22:32
13:42:21
134: 42: 21
It indicates the duration. However, the storage type is text.
Now, we need to use php to accumulate these records and display the results in seconds?
The code is as follows:
// Connect to the database...
$ Total = 0; // The total number of 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;
Here we mainly query the data and 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 sum up the seconds.
The total number of seconds is obtained.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.