function when_did($date)
{
$this_time = time();
$diff = $this_time - $date;
$num_minutes = floor($diff/60);
$num_hours = floor($diff/3600);
$num_days = floor($diff/(3600*24));
$before = 'назад';
if($num_days>0) return $num_days.' '.get_date_word($num_days, 'days').' '.$before;
if($num_hours>0) return $num_hours.' '.get_date_word($num_hours, 'hours').' '.$before;
if($num_minutes>0) return $num_minutes.' '.get_date_word($num_minutes, 'minutes').' '.$before;
return '1 минуту '.$before;
}
function get_date_word($num, $type)
{
$last_num = substr((string)$num, -1);
if($type == 'minutes')
return pad('минуту|минуты|минут', $num);
elseif($type == 'hours')
return pad('час|часа|часов', $num);
elseif($type == 'days')
return pad('день|дня|дней', $num);
return 'минуту';
}
function pad($words, $c)
{
$c = (int)$c;
$w = explode("|", $words);
if (($c%100>20 || $c%100<10) && $c%10 == 1) return $w[0];
elseif (($c%100>20 || $c%100<10) && ($c%10 > 1 && $c%10 < 5)) return $w[1];
else return $w[2];
}
CS Team