Hello,
I use the function below to show "Time ago" in forum posts. This function works fine in English, German and Spanish. For some other languages this function doesn't work correctly and I need to create separate functions.
The problem:
In English it's easy:
1 hour
2, 5 hours
1 minute
2, 5 minutes
1 second
2, 5 seconds
In some other languages is not so easy. For example, in Russian:
1 час
2 часа
5 часов
1 минуту
2 минуты
5 минут
1 секунду
2 сукунды
5 секунд
As you can see, I can't just put one letter at the and (like "s" in English) to display several hourS/minuteS/secondS.
I need something like this:
if ($hours == 1) {
$hours_lang = "час";
} elseif ($hours >= 2 && $hours <= 4) {
$hours_lang = "часа";
} else {
$hours_lang = "часов";
}
The same for minutes and seconds, and it's necessary to check hours, minutes and seconds separately because there may be timestamps like this:
1 hour 2 minutes 5 hours
I need help to alter the function below so that it doesn't become too complicated. The function use array and I have problems with it.
Thanks in advance.
function cuando($date, $set = 2)
{
global $timestamp;
$date = strtotime($date);
$calculate = $timestamp - $date;
$b = array(
'day' => 86400,
'hour' => 3600,
'minute' => 60,
'second' => 1 );
foreach ($b as $l => $data)
{
if ($calculate >= $data)
{
$time = floor( $calculate/$data );
$calculate %= $data;
$output .= ($output ? ' ' : '') . $time . ' ';
$output .= (( $time > 1) ? $l . 's' : $l );
$set--;
}
if ($set == '0')
{
break;
}
}
return $output . ' ago';
break;
}