2015-08-08 113 views
2
  1. 用戶在輸入字段中輸入了2w2d2h
  2. 在數據庫中顯示386
  3. 2w=2*24*7
  4. 2d-2*24
  5. 2h=2
  6. 加入上述三個值後,它變得386
  7. 現在來自數據庫,當我將取386它應該顯示2w2d2h

請幫我將小時轉換爲laravel中的周,日,小時4.2

截至轉換後,我現在已經插入值。

preg_match_all('#(\d[wW]+|\d[dD]+|\d[hH]+|\s+)#i', $category, $matches); 
$hours = 0; 
$days = 0; 
$hoursss = 0; 
foreach ($matches[0] AS $match) { 
    switch(true) { 
    case preg_match('/\d[hH]+|\s+/', $match): 
     $hours += (int)$match; 
     break; 

    case preg_match('/\d[dD]+|\s+/', $match): 
     $days += (int)$match * 24; 
     break; 

    case preg_match('/\d[wW]+|s+/', $match): 
     $hoursss += (int)$match * 24 * 7; 
     break; 
    } 
    $case = $hours + $hoursss + $days; 
} 
+1

我注意到相信你已做了你的問題清楚。你真的需要什麼幫助? – RiggsFolly

+0

'/ \ d + [wdh]/i'會是你的正則表達式的簡化版本。我不確定你甚至需要爲此使用正則表達式;至少不是你的程度。 –

回答

2

將下面的代碼的小時數轉換到你所需要的表達:

$hours = 386; 
$weeks = floor($hours/(24*7)); 
$hours -= $weeks * 24 * 7; 
$days = floor($hours/24); 
$hours -= $days * 24; 

printf("%dw%dd%dh", $weeks, $days, $hours);