Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
转换将秒转换为人类可读字符串的PHP函数_Php_String_Localization - Fatal编程技术网

转换将秒转换为人类可读字符串的PHP函数

转换将秒转换为人类可读字符串的PHP函数,php,string,localization,Php,String,Localization,我发现了一个将秒转换为人类可读格式的函数,如下所示: 3周2天1小时27分52秒 我想把它翻译成意大利语,所以我只翻译了数组键。现在的功能是 function secondsToHumanReadable($secs) { $units = array( 'settimane' => 604800, 'giorni' => 86400, 'ore' => 3600, 'minuti' => 60, 'secondi' =>

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

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时打印单数键

我在想,我可以将该数组与另一个具有单键的数组合并,使用

上面的数组是我需要的,但我不能使用它,因为我不能使用另一个
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
我如何实现单数单词


非常感谢您的帮助!提前非常感谢

你可以用任何你喜欢的方式来实现它们,最好不要像当前的解决方案那样缺乏清晰性和可读性(至少在我看来,带有REF和可变乒乓球的局部var突变是这样的)

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

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

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

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

CakePHP拐点类

教条拐点:

看起来很有趣:)但不幸的是,这只适用于英语
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;
}