如何编辑内爆以便它将值与两个字符串连接起来?

发布于 2024-12-07 11:03:03 字数 489 浏览 2 评论 0原文

在下面的函数中可能有一个可能的输出

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

In the function below a possible output maybe

1 day and 2 hours and 34 minutes

My question is how do I edit the implode so it will output

1 day, 2 houts and 34 minutes

This is my function

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三人与歌 2024-12-14 11:03:03

像这样的东西吗?

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

Something like this?

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 ');
若有似无的小暗淡 2024-12-14 11:03:03

我会做类似的事情:

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

I would do something like:

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" : "";
}
皓月长歌 2024-12-14 11:03:03

有很多地方可以使用 x-time-ago 函数。 此处 在 PHP 中是两个。这是 Javascript 中的一个

There are a lot of places for x-time-ago functions. here are two in PHP. Here's one in Javascript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文