下面的函数在小时
如何仅显示不为零的变量?

谢谢。

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;

最佳答案

另一种可能的方法:

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

关于php - 隐藏小时和分钟(如果它们在提供的函数中为零),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6310156/

10-10 17:13