2011-11-02 50 views
1

In this website一個PHP函數,我發現一個函數轉換秒的人類可讀的格式,例如:翻譯是轉換秒的人類可讀的字符串

3周,2天,1小時27分,52秒

我想翻譯它的意大利語,所以我剛剛翻譯了數組鍵。作用就是現在這個

function secondsToHumanReadable($secs) { 

$units = array(
'settimane' => 604800, 
'giorni' => 86400, 
'ore'  => 3600, 
'minuti' =>  60, 
'secondi' =>  1 
); 

foreach ($units as &$unit) { 
    $quot = intval($secs/$unit); 
    $secs -= $quot * $unit; 
    $unit = $quot; 
} 

return $units; 
} 

它工作得很好,但有一個小問題:在英語中所有的複數用一個字母較少的結束,但不幸的是在意大利它是不一樣的,因爲你可以看到下面。

English    Italian  
- weeks, week  - settimane, settimana 
- days, day   - giorni, giorno 
- hours, hour  - ore, ora 
- minutes, minute - minuti, minuto 
- seconds, second - secondi, secondo 

我想找到一個解決打印奇異鍵,當值== 1

我在想,我可以在陣列與另一個陣列有奇異鍵,使用array_combine()合併。

$singular_units = array(
'settimana', 
'giorno', 
'ora', 
'minuto', 
'secondo' 
); 

print_r(array_combine($singular_units, $units)); 

/* print_r: 
Array 
(
    [settimana] => 604800 
    [giorno] => 86400 
    [ora] => 3600 
    [minuto] => 60 
    [secondo] => 1 
) 
*/ 

上面的陣列是我需要的,但我不能夠使用它,因爲我只是不能使用其他foreach

$seconds = 12345*60; // just an example 
$units = secondsToHumanReadable($seconds);    
$time_string = ''; 
foreach ($units as $u => $v) 
    if (!empty($v)) 
     $time_string.= $v.' '.$u.', '; 
echo substr($time_string, 0, -2); 
// 1 settimane, 1 giorni, 13 ore, 45 minuti 
// this echo is not correct :(is expected to be like this: 
// 1 settimana, 1 giorno, 13 ore, 45 minuti 

我該如何實現單數的單詞?

任何幫助是真的讚賞!提前感謝你!

回答

3

你可以以任何你喜歡的方式來實現它們,恕我直言,最好不像目前的解決方案,缺乏清晰度和可讀性(至少這是如何local-var-mutation-with-refs-and-variable-ping-pong look喜歡我)。

只是一個可能的解決方案:

$input = 12345 * 60; 

$units = array(
    604800 => array('settimana', 'settimane'), 
    86400 => array('giorno', 'giorni'), 
    // etc 
); 

$result = array(); 
foreach($units as $divisor => $unitName) { 
    $units = intval($input/$divisor); 
    if ($units) { 
     $input %= $divisor; 
     $name = $units == 1 ? $unitName[0] : $unitName[1]; 
     $result[] = "$units $name"; 
    } 
} 

echo implode(', ', $result); 

See it in action

+0

真棒!謝謝! – Simone

0

你可能想要這樣的東西?

function secondsToHumanReadable($secs) { 

    $units = array(
    'settimane' => 604800, 
    'giorni' => 86400, 
    'ore'  => 3600, 
    'minuti' =>  60, 
    'secondi' =>  1 
    ); 

    foreach ($units as $key => &$unit) { 

     $this_unit = intval($secs/$unit); 

     $secs -= $this_unit * $unit; 

     if($this_unit == 1):  
      switch($key): 
       case "settimane": 
        $this_key = "settimana"; 
       break; 
       case "giorni": 
        $this_key = "giorno"; 
       break; 
       case "ore": 
        $this_key = "ora"; 
       break; 
       case "minuti": 
        $this_key = "minuto"; 
       break; 
       case "secondi": 
        $this_key = "secondo"; 
       break; 
      endswitch; 
     else: 
      $this_key = $key; 
     endif; 


     $results[$this_key] = $this_unit; 
    } 

    return $results; 
} 

這將返回全陣列,而不是你最初的一...與結果...

0

也是要提到的一些框架有變形器類,這將決定的複數/奇異版本一個字,但是我不確定他們是否支持意大利語等語言,但可能值得一看。我個人使用CakePHP Inflector,因爲它是一個獨立的lib,我不需要帶任何其他文件。

CakePHP的變形器類 http://api.cakephp.org/class/inflector

主義變形器:http://www.doctrine-project.org/api/common/2.0/doctrine/common/util/inflector.html

+0

看起來非常有趣:)但不幸的是只適用於英語 – Simone

相關問題