2014-05-09 36 views
0

我得到這個代碼像「4-5」和「1-7」格式化和獲取的總時間在PHP

$times = explode("-", $time); 
$thpw += ($times[1]- $times[0]); 

的問題是,當有多個時間格式像這樣的字符串工作。 。

1-2, 5-6 
or 
8-12, 1-2, 4-5, 6-7 

我該如何捕捉時間?

+1

您的預期結果是什麼? –

回答

2

使用爆炸也太,

$times = explode(",","8-12, 1-2, 4-5, 6-7"); 
$thpw = 0; 
foreach($times as $time){ 
    $time_arr = explode("-", $time); 
    $thpw += ($time_arr[1]- $time_arr[0]); 
} 
echo $thpw; // 7 

DEMO