The php record is accumulated and the result of the total length of time is displayed in seconds.

Source: Internet
Author: User

There is a duration field in the test table of the mysql database, which contains 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?Copy codeThe 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.

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.