PHP Code:
function getRemaining($from,$until){
if($until <= $from){
// Time has already elapsed
return FALSE;
}
else{
// Get time remaining
$results = array();
$time = $until - $from;
$minutes = floor($time/60);
$hoursFloat = $minutes/60;
$results['hours'] = floor($hoursFloat);
$results['minutes'] = ($hoursFloat-$results['hours'])*60;
return $results;
}
}
Returns an array with two indexes - hours and minutes, or returns false if the time supplied in $until (the time in the future) is less than that in $from. Suggested use:
PHP Code:
$array = getRemaining(time(),strtotime($futureTime));
if(!$array){
echo 'Closed';
else{
echo ( ($array['hours'] != 0) ? $array['hours'].' hour'.(($array['hours']>1) ? 's':'').(($array['minutes'] != 0) ? ' and '.$array['minutes'].' minute'.(($array['minutes']>1) ? 's': '').' remaining' : ' remaining') : (($array['minutes'] != 0) ? $array['minutes'].' minute'.(($array['minutes']>1) ? 's': '').' remaining' : ''));
}
That echo might look a bit messy, but it's just a mixture of ternaries that return a decent English sentence. It will echo a phrase like:
1 hour and 2 minutes remaining
or
2 hours and 1 minute remaining
or
2 hours and 2 minutes remaining