2015-08-08 120 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);