如果提供的函数中小时和分钟为零,则隐藏小时和分钟

发布于 2024-11-14 16:08:54 字数 460 浏览 0 评论 0原文

无论时间小于 1 小时,下面的函数都会输出“小时:0”;当分钟小于 1 时,则输出“分钟:0”。

如何只显示不为零的变量?

谢谢。

function time_difference($endtime){

    $hours =date("G",$endtime);
    $mins =date("i",$endtime);
    $secs =date("s",$endtime);
    $diff="'hours': ".$hours.",'mins': ".$mins.",'sec': ".$secs;
    return $diff;
}   

$end_time =strtotime("+7 hours") - strtotime($entry->pubDate);
$difference = time_difference($end_time);
echo $difference;

The function below outputs hours:0 whether the time is <1 hour or mins:0 when mins<1.

How can I show only the variables that are not zero?

Thank you.

function time_difference($endtime){

    $hours =date("G",$endtime);
    $mins =date("i",$endtime);
    $secs =date("s",$endtime);
    $diff="'hours': ".$hours.",'mins': ".$mins.",'sec': ".$secs;
    return $diff;
}   

$end_time =strtotime("+7 hours") - strtotime($entry->pubDate);
$difference = time_difference($end_time);
echo $difference;

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

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

发布评论

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

评论(3

缱绻入梦 2024-11-21 16:08:54

另一种可能的方法:

function time_difference($endtime){
    $times=array(
        'hours' => date("G",$endtime),
        'mins' => date("i",$endtime),
        'secs' => date("s",$endtime),
    );

    //added a "just a moment ago" feature for you
    if (intval($times['hours'], 10) == 0 
           && intval($times['mins'], 10) == 0) {
        return "just a moment ago";
    } 

    $diff='';
    foreach ($times as $k=>$v) {
        $diff.=empty($diff) ? '' : ',';
        $diff.=intval($v, 10) == 0 ? '' : "'$k':$v";
    }

    return $diff;
}   

Another possible approach:

function time_difference($endtime){
    $times=array(
        'hours' => date("G",$endtime),
        'mins' => date("i",$endtime),
        'secs' => date("s",$endtime),
    );

    //added a "just a moment ago" feature for you
    if (intval($times['hours'], 10) == 0 
           && intval($times['mins'], 10) == 0) {
        return "just a moment ago";
    } 

    $diff='';
    foreach ($times as $k=>$v) {
        $diff.=empty($diff) ? '' : ',';
        $diff.=intval($v, 10) == 0 ? '' : "'$k':$v";
    }

    return $diff;
}   
追风人 2024-11-21 16:08:54

使用?操作员。

$diff=($hours > 0) ? "'hours': ".$hours : "";
$diff=$diff.($minutes > 0) ? etc...

Use the ? operator.

$diff=($hours > 0) ? "'hours': ".$hours : "";
$diff=$diff.($minutes > 0) ? etc...
迷乱花海 2024-11-21 16:08:54

对于更大的时间范围,您最好使用数学而不是使用 date()

function time_difference($endtime){
    // hours can get over 23 now, $endtime is in seconds
    $hours = floor($endtime / 3600);
    // modulo (%) already rounds down, not need to use floor()
    $mins = $endtime / 60 % 60;
    // the remainder of $endtime / 60 are seconds in a minute
    $secs = $endtime % 60;
    // this array holds the hour, minute and seconds if greater than 0
    $diff = array();
    if ($hours) $diff[] = "'hours': $hours";
    if ($mins) $diff[] = "'mins': $mins";
    if ($secs) $diff[] = "'sec': $secs";
    // join the values with a comma
    $diff = implode(',', $diff);
    if (!$diff) { // hours, mins and secs are zero
        $diff = "just a moment ago";
    }
    return $diff;
}

以下函数仅返回 0 - 23 范围内的小时数。如果时间超过一天,小时数将变为零:

function time_difference($endtime){

    $hours = (int)date("G",$endtime);
    $mins = (int)date("i",$endtime);
    $secs = (int)date("s",$endtime);
    // this array holds the hour, minute and seconds if greater than 0
    $diff = array();
    if ($hours) $diff[] = "'hours': $hours";
    if ($mins) $diff[] = "'mins': $mins";
    if ($secs) $diff[] = "'sec': $secs";
    // join the values with a comma
    $diff = implode(',', $diff);
    if (!$diff) { // hours, mins and secs are zero
        $diff = "just a moment ago";
    }
    return $diff;
}

需要 (int)date() 返回的字符串转换为字符串。使用此“01”变为1,“00”变为“0”。

For larger time ranges, you'd better use maths instead of using date():

function time_difference($endtime){
    // hours can get over 23 now, $endtime is in seconds
    $hours = floor($endtime / 3600);
    // modulo (%) already rounds down, not need to use floor()
    $mins = $endtime / 60 % 60;
    // the remainder of $endtime / 60 are seconds in a minute
    $secs = $endtime % 60;
    // this array holds the hour, minute and seconds if greater than 0
    $diff = array();
    if ($hours) $diff[] = "'hours': $hours";
    if ($mins) $diff[] = "'mins': $mins";
    if ($secs) $diff[] = "'sec': $secs";
    // join the values with a comma
    $diff = implode(',', $diff);
    if (!$diff) { // hours, mins and secs are zero
        $diff = "just a moment ago";
    }
    return $diff;
}

The below function would only return hours in the range 0 - 23. If the time exceeds a day, hours become zero:

function time_difference($endtime){

    $hours = (int)date("G",$endtime);
    $mins = (int)date("i",$endtime);
    $secs = (int)date("s",$endtime);
    // this array holds the hour, minute and seconds if greater than 0
    $diff = array();
    if ($hours) $diff[] = "'hours': $hours";
    if ($mins) $diff[] = "'mins': $mins";
    if ($secs) $diff[] = "'sec': $secs";
    // join the values with a comma
    $diff = implode(',', $diff);
    if (!$diff) { // hours, mins and secs are zero
        $diff = "just a moment ago";
    }
    return $diff;
}

(int) is needed to turn the string returned by date() into a string. "01" becomes 1 and "00" becomes "0" using this.

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