如何编辑implode以便它将两个字符串连接起来?

时间:2011-09-30 17:19:56

标签: php function implode

在下面的函数中可能输出

1 day and 2 hours and 34 minutes

我的问题是如何编辑内爆以便输出

1 day, 2 houts and 34 minutes

这是我的功能

function time_difference($endtime){
    $hours = (int)date("G",$endtime);
    $mins = (int)date("i",$endtime);

    // join the values
    $diff = implode(' and ', $diff);

    if (($hours == 0 ) && ($mins == 0)) {
        $diff = "few seconds ago";
    }
    return $diff;
}

3 个答案:

答案 0 :(得分:1)

这样的东西?

function implodeEx($glue, $pieces, $glueEx = null)
{
    if ($glueEx === null)
        return implode($glue, $pieces);
    $c = count($pieces);
    if ($c <= 2)
        return implode($glueEx, $pieces);

    $lastPiece = array_pop($pieces);
    return implode($glue, array_splice($pieces, 0, $c - 1)) . $glueEx . $lastPiece;
}

$a = array('a', 'b', 'c', 'd', 'e');
echo implodeEx(',', $a, ' and ');

答案 1 :(得分:0)

x-time-before功能有很多地方。 PHP中heretwo。这是Javascript中的one

答案 2 :(得分:0)

我会做类似的事情:

if ($days) {
    $diff .= "$days day";
    $diff .= $days > 1 ? "s" : "";
}
if ($hours) {
    $diff .= $diff ? ", " : "";
    $diff .= "$hours hour";
    $diff .= $hours > 1 ? "s" : "";
}
if ($mins) {
    $diff .= $diff ? " and " : "";
    $diff .= "$mins minute";
    $diff .= $mins > 1 ? "s" : "";
}