php使用humanTiming函数查找事件开始前的时间


php using humanTiming function to find the time until an event starts

我正在使用这个函数

function humanTiming ($time)
{
    $time = $time - time() ; // to get the time since that moment
    $time = ($time<1)? 1 : $time;
    $tokens = array (
    31536000 => 'year',
    2592000 => 'month',
    604800 => 'week',
    86400 => 'day',
    3600 => 'hour',
    60 => 'minute',
    1 => 'second',
    );
    foreach ($tokens as $unit => $text) 
    {
        if ($time < $unit) continue;
            $numberOfUnits = floor($time / $unit);
            return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
    }
}

是原始的一个稍微修改的版本,因为我将这些交换到$time - time()左右,以便该函数将给我一个结果,该结果将告诉我直到特定日期的时间,而不是特定日期的时间。

我想做的是当时间小于1秒时显示"过期"而不是当前默认的"1秒"

我该怎么做呢?多谢

《路加福音》

$time = ($time<1)? 1 : $time; 

替换为

if ($time < 1) { return 'expired';} 

可以在循环开始前执行:

function humanTiming ($time)
    {
        $time = $time - time() ; // to get the time since that moment
        $time = ($time<1)? 1 : $time;
        $tokens = array (
        31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second',
        );
        if($time < 1 ) return 'expired';
        foreach ($tokens as $unit => $text) 
        {
            if ($time < $unit) continue;
                $numberOfUnits = floor($time / $unit);
                return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
        }
    }