翻译一个PHP函数,将秒数转换为人类可读的字符串


Translating a PHP function that converts seconds to human readable string

在这个网站上,我发现了一个将秒转换为人类可读格式的函数,如下所示:

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

如何实现单数单词?

任何帮助是真的感激!提前谢谢你!

你可以以任何你喜欢的方式实现它们,我认为最好不要像当前的解决方案那样缺乏清晰度和可读性(至少这是我看到的local-var-mutation-with-refs-and-variable- pong的样子)。

只有一个可能的解决方案:

$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);

查看效果

您可能想要这样的东西?

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

这将返回完整的数组,而不是您的初始数组…结果…

我还想提到一些框架有Inflector类,它将决定一个单词的复数/单数版本,但是我不确定它们是否支持意大利语等语言,但它可能值得一看。我个人使用CakePHP Inflector,因为它是一个独立的库,我不需要引入任何其他文件。

CakePHP Inflector类http://api.cakephp.org/class/inflector

Doctrine Inflector: http://www.doctrine-project.org/api/common/2.0/doctrine/common/util/inflector.html