【问题标题】:Hide hours and mins if they are zero in the function provided如果在提供的功能中为零,则隐藏小时和分钟
【发布时间】:2011-06-10 17:45:57
【问题描述】:

下面的函数输出 hours: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;

【问题讨论】:

  • 零变量不为空。它们有价值;该值为零。
  • @Paul 实际上是一个值为 0 的变量,在 php 中被认为是 empty :)。
  • @bazmegakapa 在所有版本中?
  • 自 PHP 时代开始 :)。见empty() in Manual
  • 这是一个很好的知识。其他语言也一样吗?

标签: php date string if-statement


【解决方案1】:

另一种可能的方法:

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

【讨论】:

  • 拥有一个 foreach 循环比上面的解决方案之类的数组更快?
  • @Punkis 考虑到两个代码之间的差异,很难判断哪个更快,并且在这个级别上并不重要(那将是微优化)。可读性更重要。
  • @Punkis 为您添加了“刚刚”。
  • 感谢您的信息。我已经对其进行了测试,我的一个帖子显示为 'mins':00,'secs':46 (它应该是“只是片刻”。以下帖子显示为 'mins':03,'secs':46 如果它有帮助。
  • @Punkis 哦,是的,我忘了date 会添加填充零,所以empty 不会正确解释它。让我快速更正一下。
【解决方案2】:

使用 ?运算符。

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

【讨论】:

  • 我的错误。如果经过的时间
  • 最后做: if($hours
  • 你的意思是这样?我没有得到预期的结果。 $diff=($hours > 0) ? "'hours': ".$hours : ""; $diff=$diff.($mins > 0) ? "'mins': ".$mins : ""; $diff=$diff.($secs > 0) ? "'secs': ".$secs : ""; if($hours < 1 && $minutes < 1 && $seconds < 1) $diff = "Just a moment ago";
  • 抱歉删除了“&& $seconds
【解决方案3】:

对于更大的时间范围,您最好使用数学而不是使用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”。

【讨论】:

  • 嗨 Lekensteyn,当小时和分钟为空时,如何显示“刚刚”?
  • @mtopia:如果您需要几天,您可能不想截断时间。我在答案中添加了新代码。如果要显示日期,请将$hours = floor($endtime / 3600); 替换为$days = floor($endtime / 3600 / 24);$hours = $endtime / 3600 / 24 % 24; 并在if ($hours) 之前添加if ($days) $diff[] = "'days': $days";
猜你喜欢
  • 1970-01-01
  • 2020-03-05
  • 2021-11-04
  • 1970-01-01
  • 2021-11-13
  • 2016-11-06
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多