How to store data for a field with a storage time

Source: Internet
Author: User
Tags strtok
How to store data in a time field? there is a field that requires storage time, and all the values of this field need to be added, and then converted to the correct time. For example

5.45 minutes and 45 seconds for storage
2,120 minutes 01 seconds storage is 120.01
89.19 for minutes and 19 seconds

=====

The sum is 5.45 + 120.01 + 89.19 = 214.65, but it is different from the original value.

Is there any good way to solve this problem? I don't think it can be stored like this. for example, it is very reliable to convert all the time input by users into seconds and store them in the database, simply add the number of seconds. No errors.

But how can we convert the value entered by the user into seconds?


Reply to discussion (solution)

$ Input = 1234.5678; strtok (string) $ input ,'. '); $ s = strtok (string) $ input ,'. ') * 60 + strtok ('. '); echo $ s. 'second ';

$ Input = 1234.5678; $ s = strtok (string) $ input, '.') * 60 + strtok ('.'); echo $ s. 'second ';

$ Input = 1234.5678; $ s = strtok (string) $ input, '.') * 60 + strtok ('.'); echo $ s. 'second ';



Well, I see, what if there is an hour? for example, it's 3H45. 26, that is, 3 hours, 45 minutes, and 26 seconds.
But sometimes there are only minutes without hours. in this case, you cannot convert the hour into minutes by yourself. do you need to enter 205.26 for three hours and 45 minutes ~

I just don't know how to do it, and I have no idea.

The author studies the strtotime () function to see if it can be implemented.

5.45 + 120.01 + 89.19 = 214.65
60? 10? System Plus ,? But no ?.

? All first? Seconds ?, And then add.

$t = array('3H45.26','1H5.10','30.59');echo sumT($t); // 5H21.35function sumT($t){$sum = 0;if($t){foreach($t as $v){$sum += getSecond($v);}}return tostr($sum);}function getSecond($str){if(strstr($str,'H')==''){$str = '0H'.$str;}$str = str_replace('H','.',$str);list($h, $m, $s) = explode('.', $str);return $h*3600+$m*60+$s;}function tostr($t){$h = (int)($t/3600);$m = (int)($t%3600/60);$s = $t%3600%60;$h = $h>0? $h.'H' : '';return $h.$m.'.'.$s;}

Ask the user to write 034526 in 3 hours, 45 minutes, 26 seconds. isn't that too much? Easier than yours
Therefore

echo strtotime('034526') - strtotime(date('Y-m-d')), 
13526

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.