BBLJ84 BBLJ84 - 23 days ago 6
PHP Question

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

I am using this function

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


which is a slightly modified version of the original as i swapped these to around $time - time() so that the function would give me a result which would tell me a time until a specific date is not a time when a specific date was.

what i am trying to do is when the time is less than 1 second have it display "expired" instead of the current default "1 second"

any ideas how i can do this?
Many thanks

Luke

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

replace with

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