一個儲存時間的欄位如何儲存資料

來源:互聯網
上載者:User
一個儲存時間的欄位怎麼儲存資料
有一個欄位需要儲存時間,並且需要把這個欄位所有的值相加,然後換算成正確的時間。比如

1,5分鐘45秒 儲存為 5.45
2,120分鐘01秒 儲存為120.01
3,89分鐘19秒 儲存為89.19

=====

合計起來就是 5.45+120.01+89.19=214.65 但是和本來的數值不一樣的

有沒有什麼好辦法來解決這個問題呢,我覺得不能儲存成這樣,比如把使用者輸入的時間數全部轉換成秒數儲存到資料庫,這樣靠譜點,直接秒數相加就可以了。也不會出錯。

但是怎麼樣把使用者輸入的數值轉換成秒數呢
------解決方案--------------------

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

------解決方案--------------------

$t = array('3H45.26','1H5.10','30.59');

echo sumT($t); // 5H21.35

function 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;
}

------解決方案--------------------
要求使用者將 3小時45分鐘26秒 寫作 034526 這不過分吧?比你的還簡單
於是有
echo strtotime('034526') - strtotime(date('Y-m-d')), 
13526
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.