Helper timeAgoInWords
Převede čas na slovní vyjádření v češtině.
| Verze | 1 |
| Autor | David Grudl |
| Licence | New BSD License |
Instalace
Následující třídu si zkopírujte do své aplikace:
class Helpers
{
public static function timeAgoInWords($time)
{
if (!$time) {
return FALSE;
} elseif (is_numeric($time)) {
$time = (int) $time;
} elseif ($time instanceof DateTime) {
$time = $time->format('U');
} else {
$time = strtotime($time);
}
$delta = time() - $time;
if ($delta < 0) {
$delta = round(abs($delta) / 60);
if ($delta == 0) return 'za okamžik';
if ($delta == 1) return 'za minutu';
if ($delta < 45) return 'za ' . $delta . ' ' . self::plural($delta, 'minuta', 'minuty', 'minut');
if ($delta < 90) return 'za hodinu';
if ($delta < 1440) return 'za ' . round($delta / 60) . ' ' . self::plural(round($delta / 60), 'hodina', 'hodiny', 'hodin');
if ($delta < 2880) return 'zítra';
if ($delta < 43200) return 'za ' . round($delta / 1440) . ' ' . self::plural(round($delta / 1440), 'den', 'dny', 'dní');
if ($delta < 86400) return 'za měsíc';
if ($delta < 525960) return 'za ' . round($delta / 43200) . ' ' . self::plural(round($delta / 43200), 'měsíc', 'měsíce', 'měsíců');
if ($delta < 1051920) return 'za rok';
return 'za ' . round($delta / 525960) . ' ' . self::plural(round($delta / 525960), 'rok', 'roky', 'let');
}
$delta = round($delta / 60);
if ($delta == 0) return 'před okamžikem';
if ($delta == 1) return 'před minutou';
if ($delta < 45) return "před $delta minutami";
if ($delta < 90) return 'před hodinou';
if ($delta < 1440) return 'před ' . round($delta / 60) . ' hodinami';
if ($delta < 2880) return 'včera';
if ($delta < 43200) return 'před ' . round($delta / 1440) . ' dny';
if ($delta < 86400) return 'před měsícem';
if ($delta < 525960) return 'před ' . round($delta / 43200) . ' měsíci';
if ($delta < 1051920) return 'před rokem';
return 'před ' . round($delta / 525960) . ' lety';
}
/**
* Plural: three forms, special cases for 1 and 2, 3, 4.
* (Slavic family: Slovak, Czech)
* @param int
* @return mixed
*/
private static function plural($n)
{
$args = func_get_args();
return $args[($n == 1) ? 1 : (($n >= 2 && $n <= 4) ? 2 : 3)];
}
}
Příklad použití
Funkci zaregistrujete do šablony příkazem:
$template->registerHelper('timeAgoInWords', 'Helpers::timeAgoInWords');
Poté stačí v šabloně použít:
Odesláno {$time|timeAgoInWords}
Připojené soubory
- helpers.php 3 kB
Komentáře 
Wizzard256 | 3. 10. 2011, 9:57 | comment
Tady je to anglicky:
class Helpers {
public static function ago($timestamp) {
$difference = time() - $timestamp;
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
for($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] ago";
return $text;
}
}

Wizzard256 | 30. 9. 2011, 16:50 | question
nema to nekdo v anglictine? potreboval bych to v anglictine.. :-)