The PHP record is cumulative and displays the total time length as a second result _php tips

Source: Internet
Author: User
Tags explode
Now there is a MySQL database in the test table has a Duration field, which has three records:
00:22:32
13:42:21
134:42:21

Represents the time length, however, the save type is text.

Now, how do you add these records in PHP and end up as a result of a total length of seconds?
Copy Code code as follows:

Connect database ... Slightly
$total = 0; 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;


This is mainly to query the data, and then use the Explode function to ":" Split the string, get an array.
Then calculate the hours corresponding to the number of seconds, minutes corresponding to the number of seconds. And then add up the number of seconds.
Finally, the total number of seconds is obtained.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.